This library is derive macros for Try
trait version 2. Try
trait version 2 is experimental in nightly version. You need a nightly compiler.
It has two macros for newtypes:
IdTry
: It implements infallibleTry
trait. It can be used likeIdentity
monad.Try
: It implementsTry
trait working through inner type.
#[derive(IdTry)]
struct Id<T>(T);
fn id_sample() -> Id<u8> {
Id(Id(1)? + Id(2)? + Id(3)?)
}
#[derive(Try)]
struct NewResult<T, E>(Result<T, E>);
impl<T, E> NewResult<T, E> {
fn new(value: T) -> Self {
Self(Ok(value))
}
}
fn new_result_sample() -> NewResult<u8, String> {
NewResult::new(NewResult::new(1)? + NewResult::new(2)? + NewResult::new(3)?)
}
#[derive(Try)]
struct New<T: ops::Try>(T);
fn new_sample() -> New<Option<u8>> {
New(Some(New(Some(1))? + New(Some(2))? + New(Some(3))?))
}
I made hand-made and macro-generated versions:
I made these macros to use Try
trait on Tagless Final encoding in Rust. I created some docs: