Skip to main content

Crate small_read_only

Crate small_read_only 

Source
Expand description

§Example

use small_read_only::ReadOnly;
 
#[derive(ReadOnly)]
pub struct A<'a> {
    b: usize,
    c: String,
    d: &'a str,
}
 
impl<'a> A<'a> {
    pub fn new(b: usize, c: String, d: &'a str) -> Self {
        Self {
            b, c, d
        }
    }
}
 
let a = A::new(1, "c".to_string(), "d");
 
assert_eq!(a.b(), &1);
assert_eq!(a.c(), "c");
assert_eq!(a.d(), &"d");

§NoRead attribute

#[derive(ReadOnly)]
pub struct B {
    #[NoRead]
    b: usize,
}
 
impl B {
    pub fn new(b: usize) -> Self {
        Self {
            b
        }
    }
}
 
let b = B::new(1);
b.b();

Derive Macros§

ReadOnly
Implement the getters for all fields of a struct