By default, fields of a struct in Rust are private, but add visibility for the fields one by one is annoying, so the viewit
attribute macro will help you do such things.
Without viewit
:
rust
pub struct Foo {
pub f1: u8,
pub f2: u16,
pub f3: String,
pub f4: Vec<u8>
}
With viewit
:
```rust
use viewit::viewit;
pub struct Foo {
f1: u8,
f2: u16,
f3: String,
f4: Vec
By default, viewit
will use the struct's visibility for each field, and the expand code is equal to the below.
rust
pub struct Foo {
pub f1: u8,
pub f2: u16,
pub f3: String,
pub f4: Vec<u8>,
}
impl Foo {
#[inline]
pub fn f1(&self) -> &u8 {
&self.f1
}
#[inline]
pub fn f2(&self) -> &u16 {
&self.f2
}
#[inline]
pub fn f3(&self) -> &String {
&self.f3
}
#[inline]
pub fn f4(&self) -> &Vec<u8> {
&self.f4
}
#[inline]
pub fn set_f1(mut self, val: u8) -> Self {
self.f1 = val;
self
}
#[inline]
pub fn set_f2(mut self, val: u16) -> Self {
self.f2 = val;
self
}
#[inline]
pub fn set_f3(mut self, val: String) -> Self {
self.f3 = val;
self
}
#[inline]
pub fn set_f4(mut self, val: Vec<u8>) -> Self {
self.f4 = val;
self
}
}
viewit
have mutliple useful configs to help you generate what you want flexibly.
```rust
use viewit::viewit;
struct FromString { src: String, }
impl From<&String> for FromString { fn from(src: &String) -> Self { Self { src: src.clone() } } }
fn vectostring(src: &[u8]) -> String { String::fromutf8lossy(src).to_string() }
// set this, then this visibility will be applied to the fields visall = "pub(crate)", // this will not generate setters setters( // change the prefix for all setters prefix = "with", // change the setters fn style, available values here are ref, into, tryinto or move style = "ref", // if you do not want to generate getters, you can use skip // skip, ), getters( // change the prefix for all getters prefix = "get", // change the getters fn style, available values here are ref and move style = "ref", // if you do not want to generate getters, you can use skip // skip, ), // print the generated code to std out, other available values here are: stderr or "path/to/output/file" debug = "stdout" )] struct Foo { #[viewit( getter( style = "move", rename = "getfirst_field", vis = "pub" // we can custom field getter ), setter( skip, // we do not want the setter for the field, then we skip it. ) )] f1: u8, #[viewit( getter( skip, // we do not want the getter for the field, then we skip it ) )] f2: u16,
#[viewit( getter( result( // sometimes we may want to convert the f4 field to a generic type type = "T", converter( style = "ref", // take the ownership of the field fn = "T::from", // the fn used to do the conversion ), // set the trait bound bound = "T: for<'a> From<&'a String>" ) ) )] f3: String,
#[viewit(
getter(
result(
// we want to convert the f3 field to String
type = "String",
converter(
style = "ref", // take the reference of the field
fn = "vectostring" // the fn used to do the conversion
),
)
)
)]
f4: Vec
viewit
will help you to generate the code:
rust
struct Foo {
pub(crate) f1: u8,
pub(crate) f2: u16,
pub(crate) f3: String,
pub(crate) f4: Vec<u8>,
}
impl Foo {
#[inline]
pub fn get_first_field(&self) -> u8 {
self.f1
}
#[inline]
pub(crate) fn get_f3<T: for<'a> From<&'a String>>(&self) -> T {
T::from(&self.f3)
}
#[inline]
pub(crate) fn get_f4(&self) -> String {
vec_to_string(&self.f4)
}
#[inline]
pub(crate) fn with_f2(&mut self, val: u16) {
self.f2 = val;
}
#[inline]
pub(crate) fn with_f3(&mut self, val: String) {
self.f3 = val;
}
#[inline]
pub(crate) fn with_f4(&mut self, val: Vec<u8>) {
self.f4 = val;
}
}
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.