Skip to content

Building on stable #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(try_trait, alloc_layout_extra)]
#![forbid(missing_docs)]

/*!
Expand Down Expand Up @@ -172,7 +171,9 @@ impl<F: FnOnce()> Drop for OnDrop<F> {
}

mod boxed;
mod r#try;
mod vec;

pub use self::boxed::*;
pub use self::r#try::*;
pub use self::vec::*;
68 changes: 68 additions & 0 deletions src/try.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// A stable version of [`core::ops::Try`].
pub trait Try {
/// The type of this value when viewed as successful.
type Ok;
/// The type of this value when viewed as failed.
type Error;

/// A return of `Ok(t)` means that the
/// execution should continue normally, and the result of `?` is the
/// value `t`. A return of `Err(e)` means that execution should branch
/// to the innermost enclosing `catch`, or return from the function.
fn into_result(self) -> Result<Self::Ok, Self::Error>;

/// Wrap an error value to construct the composite result. For example,
/// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
fn from_error(v: Self::Error) -> Self;

/// Wrap an OK value to construct the composite result. For example,
/// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
fn from_ok(v: Self::Ok) -> Self;
}

impl<T, E> Try for Result<T, E> {
type Ok = T;
type Error = E;

fn into_result(self) -> Result<<Self as Try>::Ok, <Self as Try>::Error> {
self
}
fn from_error(v: <Self as Try>::Error) -> Self {
Err(v)
}
fn from_ok(v: <Self as Try>::Ok) -> Self {
Ok(v)
}
}

/// The error type that results from applying the try operator (`?`) to a `None` value.
pub struct NoneError;

impl<T> Try for Option<T> {
type Ok = T;
type Error = NoneError;

fn into_result(self) -> Result<<Self as Try>::Ok, <Self as Try>::Error> {
self.ok_or(NoneError)
}
fn from_error(_v: <Self as Try>::Error) -> Self {
None
}
fn from_ok(v: <Self as Try>::Ok) -> Self {
Some(v)
}
}

/// Unwraps a result or propagates its error.
#[macro_export]
macro_rules! r#try {
($expr:expr) => {
match $crate::Try::into_result($expr) {
Ok(val) => val,
Err(err) => return $crate::Try::from_error(::core::convert::From::from(err)),
}
};
($expr:expr,) => {
$crate::r#try!($expr)
};
}
8 changes: 4 additions & 4 deletions src/vec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::alloc::Layout;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::Try;

use std::alloc::Layout;
use super::{r#try, Try};

mod general_zip;

Expand Down Expand Up @@ -214,7 +214,7 @@ impl<T, U> MapIter<T, U> {
// does a pointer walk, easy for LLVM to optimize
while self.init_len < self.data.len {
unsafe {
let value = f(self.data.ptr.read())?;
let value = r#try!(f(self.data.ptr.read()));

(self.data.ptr as *mut U).write(value);

Expand Down Expand Up @@ -303,7 +303,7 @@ impl<T, U, V> ZipWithIter<T, U, V> {
self.left.ptr = self.left.ptr.add(1);
self.right.ptr = self.right.ptr.add(1);

let value = f(left.read(), right.read())?;
let value = r#try!(f(left.read(), right.read()));

out.write(value);
}
Expand Down
8 changes: 3 additions & 5 deletions src/vec/general_zip.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::{Input, Output};

#[doc(hidden)]
pub use std::ops::Try;
use std::alloc::Layout;

use super::{r#try, Input, Output, Try};

use seal::Seal;
mod seal {
use super::*;
Expand Down Expand Up @@ -402,7 +400,7 @@ impl<V, In: Tuple> ZipWithIter<V, In> {

let input = In::next_unchecked(&mut self.input);

self.output.ptr.write(f(input)?);
self.output.ptr.write(r#try!(f(input)));
self.output.ptr = self.output.ptr.add(1);
}

Expand Down