Skip to content

pismute/derive_try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

derive_try

Build Status GitHub license Rust Nightly

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 infallible Try trait. It can be used like Identity monad.
  • Try: It implements Try trait working through inner type.

Examples

#[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:

Docs

I made these macros to use Try trait on Tagless Final encoding in Rust. I created some docs:

About

Derive macros for Try

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages