Skip to main content

define_restricted_wrapper

Macro define_restricted_wrapper 

Source
macro_rules! define_restricted_wrapper {
    ($(#[$meta:meta])* $name:ident: $(#[$inner_meta:meta])* $inner:path $(= $default:expr)?) => { ... };
}
Expand description

Implement a restricted wrapper around an inner type:

Implements Deref, PartialEq, Clone, Debug, Serialize.

ยงExample

use cba::define_restricted_wrapper;

#[cfg(feature = "serde")] {
    define_restricted_wrapper!(Percentage: u16 = 100);
    impl Percentage {
        pub fn new(value: u16) -> Self {
            if value <= 100 { Self(value) } else { Self(100) }
        }
    }
}