#getters #read #struct #macro #fields

macro small_read_only

A macro to implement getters on a struct

1 unstable release

0.1.0 Aug 29, 2024

#1472 in Procedural macros

Download history 116/week @ 2024-08-29 17/week @ 2024-09-12 11/week @ 2024-09-19 15/week @ 2024-09-26 10/week @ 2024-10-03

53 downloads per month

MIT/Apache

5KB

small_read_only

This crate adds #[derive(ReadOnly)].
It implements getters for all fields without the #[NoRead] attribute.

It works for:

  • Structs

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");

License

MIT or Apache-2.0


lib.rs:

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();

Dependencies

~245–690KB
~16K SLoC