Skip to content

A simple error library for no_std + no_alloc Rust

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
Apache-2.0
LICENSE-MIT
Notifications You must be signed in to change notification settings

richardanaya/no_error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

no_error

docs.rs docs

An error library for no_std + no_alloc Rust

  • macro transforms string literals into C style character array
  • no allocator required
  • support for non-C string text (i.e. len + string)
  • support for error codes if text isn't an option
use no_error::*;

extern "C" {
    fn print(x: const *u8); // takes in a c-string
    fn print_with_len(x: const *u8, len usize); // takes in text and length
}

const FAIL:ErrorCode = 42;

fn can_fail(i:i32) -> Result<()> {
    if i < 0 { 
        // programmatically appends a "/0" to end of static string
        error_message!("a failure happened","it happened in can_fail()")
    } else if i == 0 {
        // don't like c strings? supports failure codes too
        error_code!(FAIL)
    } else {
        // you don't have to specify the source if you don't want
        error_message!("a failure happened")
    }
}

fn main() {
    match can_fail(42) {
        Ok(_) => (),
        Err(a) => {
            print(a.cstr_description());
            let source = a.source();
            print_with_len(source.as_ptr(),source.len());
            if let Some(c) = a.code() {
                if c == FAIL {
                    print("secret of life".as_ptr());
                }
            }
        },
    };
}

About

A simple error library for no_std + no_alloc Rust

Resources

License

Apache-2.0, Apache-2.0 licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
Apache-2.0
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published