#panic #operator #div #occurs

nopanick

No panic library for add, sub, mul, div operators in Rust

4 releases

0.2.0 Nov 11, 2023
0.1.7 Jan 30, 2023

#3 in #div

MIT/Apache

9KB
73 lines

nopanick-rs (No Panic!)

No panic library for add, sub, mul, div operators in Rust.

example

use nopanick::*;

fn main() {
    unsafe {
        println!("{}", div!(sub!(add!(100i32, 10i32), 5i32), 5i32));
    }
}

Building

Release only for optimization effect:

cargo build --release

Contact me


lib.rs:

nopanick-rs (No Panic!)

No panic library for add, sub, mul, div operators in Rust.

Generates less instructions, suite for barematal or embedded system.

Example#1

use nopanick::*;

fn main() {
    unsafe {
        println!("{}", div!(sub!(add!(100i32, 10i32), 5i32), 5i32));
    }
}

Example#2

How to use .unwarp_nop() with Option<T>

use nopanick::*;

fn rem(a: u32, b: u32) -> Option<u32> {
    if b != 0 {
        Some(a % b)
    } else {
        None
    }
}

fn main() {
    let a = 4u32;
    let b = 3u32;
    let r = unsafe { rem(a, b).unwrap_nop() };

    println!("{} % {} ={}", a, b, r);
}

Example#3

If None was unwraped, UB occurs.

use nopanick::*;

fn rem(a: u32, b: u32) -> Option<u32> {
    if b != 0 {
        Some(a % b)
    } else {
        None
    }
}

fn main() {
    let a = 4u32;
    let b = 0u32;
    let r = unsafe { rem(a, b).unwrap_nop() };

    println!("{} % {} ={}", a, b, r);
}
//error: process didn't exit successfully: `target\debug\examples\option.exe` (exit code: 0xc000001d, STATUS_ILLEGAL_INSTRUCTION) Illegal instruction

No runtime deps