pub struct Field<'a> { /* private fields */ }Expand description
An immutable struct field reference.
A FieldRef is a proxy for immutable operations on a struct’s field.
Values of this type are created by FieldAccess::field.
Implementations§
Source§impl<'a> Field<'a>
impl<'a> Field<'a>
Sourcepub fn is<T: Any>(&self) -> bool
pub fn is<T: Any>(&self) -> bool
Returns true if the field is of type T.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let foo = Foo { a: 1 };
let field = foo.field("a").unwrap();
assert!(field.is::<u8>());
assert!(!field.is::<&str>());Sourcepub fn type_id(&self) -> TypeId
pub fn type_id(&self) -> TypeId
Gets the TypeId of the field’s value.
§Example
use core::any::TypeId;
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let foo = Foo { a: 1 };
let field = foo.field("a").unwrap();
assert_eq!(field.type_id(), TypeId::of::<u8>());Sourcepub fn get<T: Any>(&self) -> Option<&T>
pub fn get<T: Any>(&self) -> Option<&T>
Obtains an immutable reference to the value of type T.
Returns Some(_) if field’s value is of type T, None otherwise.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let foo = Foo { a: 42 };
let field = foo.field("a").unwrap();
assert_eq!(field.get::<u8>(), Some(&42u8));
assert_eq!(field.get::<&str>(), None);Sourcepub fn as_any(&self) -> &dyn Any
pub fn as_any(&self) -> &dyn Any
Obtains an immutable reference to the value as &dyn Any.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8
}
let foo = Foo { a: 42 };
let field = foo.field("a").unwrap();
let any = field.as_any();
assert_eq!(any.downcast_ref::<u8>(), Some(&42u8));Sourcepub fn is_slice<T: Any>(&self) -> bool
pub fn is_slice<T: Any>(&self) -> bool
Returns true if the field value is of type &[T].
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: &'static [u8]
}
let foo = Foo { a: &[1, 2, 3] };
let field = foo.field("a").unwrap();
assert!(field.is_slice::<u8>());Sourcepub fn as_slice<T: Any>(&self) -> Option<&[T]>
pub fn as_slice<T: Any>(&self) -> Option<&[T]>
Obtain an immutable reference to the value as &[T].
Returns Some(_) if .is_slice::<T>() or
.is_vec::<T>() would return true, None otherwise.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: Vec<u8>
}
let foo = Foo { a: vec![1, 2, 3] };
let field = foo.field("a").unwrap();
assert_eq!(field.as_slice(), Some(&[1u8, 2, 3][..]));Sourcepub fn is_vec<T: Any>(&self) -> bool
pub fn is_vec<T: Any>(&self) -> bool
Returns true if the field value is of type Vec<T>.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: Vec<u8>
}
let foo = Foo { a: vec![1, 2, 3] };
let field = foo.field("a").unwrap();
assert!(field.is_vec::<u8>());
assert!(!field.is_vec::<u16>());Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Returns true if the field value is of type String.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: String,
}
let foo = Foo { a: String::from("bar") };
let field = foo.field("a").unwrap();
assert!(field.is_string());
assert!(!field.is_str());Sourcepub fn is_str(&self) -> bool
pub fn is_str(&self) -> bool
Returns true if the field value is of type &str.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: &'static str,
}
let foo = Foo { a: "bar" };
let field = foo.field("a").unwrap();
assert!(field.is_str());Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Obtain an immutable reference to the value as &str.
Returns Some(_) if .is_str() or .is_string() would
return true, None otherwise.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: String
}
let foo = Foo { a: String::from("bar") };
let field = foo.field("a").unwrap();
assert_eq!(field.as_str(), Some("bar"));Sourcepub fn is_bool(&self) -> bool
pub fn is_bool(&self) -> bool
Returns true if the field value is of type bool.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: bool,
}
let foo = Foo { a: bool::default() };
let field = foo.field("a").unwrap();
assert!(field.is_bool());
assert!(!field.is::<&bool>());Sourcepub fn is_u8(&self) -> bool
pub fn is_u8(&self) -> bool
Returns true if the field value is of type u8.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u8,
}
let foo = Foo { a: u8::default() };
let field = foo.field("a").unwrap();
assert!(field.is_u8());
assert!(!field.is::<&u8>());Sourcepub fn is_u16(&self) -> bool
pub fn is_u16(&self) -> bool
Returns true if the field value is of type u16.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u16,
}
let foo = Foo { a: u16::default() };
let field = foo.field("a").unwrap();
assert!(field.is_u16());
assert!(!field.is::<&u16>());Sourcepub fn is_u32(&self) -> bool
pub fn is_u32(&self) -> bool
Returns true if the field value is of type u32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u32,
}
let foo = Foo { a: u32::default() };
let field = foo.field("a").unwrap();
assert!(field.is_u32());
assert!(!field.is::<&u32>());Sourcepub fn is_u64(&self) -> bool
pub fn is_u64(&self) -> bool
Returns true if the field value is of type u64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u64,
}
let foo = Foo { a: u64::default() };
let field = foo.field("a").unwrap();
assert!(field.is_u64());
assert!(!field.is::<&u64>());Sourcepub fn is_u128(&self) -> bool
pub fn is_u128(&self) -> bool
Returns true if the field value is of type u128.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: u128,
}
let foo = Foo { a: u128::default() };
let field = foo.field("a").unwrap();
assert!(field.is_u128());
assert!(!field.is::<&u128>());Sourcepub fn is_usize(&self) -> bool
pub fn is_usize(&self) -> bool
Returns true if the field value is of type usize.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: usize,
}
let foo = Foo { a: usize::default() };
let field = foo.field("a").unwrap();
assert!(field.is_usize());
assert!(!field.is::<&usize>());Sourcepub fn is_i8(&self) -> bool
pub fn is_i8(&self) -> bool
Returns true if the field value is of type i8.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: i8,
}
let foo = Foo { a: i8::default() };
let field = foo.field("a").unwrap();
assert!(field.is_i8());
assert!(!field.is::<&i8>());Sourcepub fn is_i16(&self) -> bool
pub fn is_i16(&self) -> bool
Returns true if the field value is of type i16.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: i16,
}
let foo = Foo { a: i16::default() };
let field = foo.field("a").unwrap();
assert!(field.is_i16());
assert!(!field.is::<&i16>());Sourcepub fn is_i32(&self) -> bool
pub fn is_i32(&self) -> bool
Returns true if the field value is of type i32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: i32,
}
let foo = Foo { a: i32::default() };
let field = foo.field("a").unwrap();
assert!(field.is_i32());
assert!(!field.is::<&i32>());Sourcepub fn is_i64(&self) -> bool
pub fn is_i64(&self) -> bool
Returns true if the field value is of type i64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: i64,
}
let foo = Foo { a: i64::default() };
let field = foo.field("a").unwrap();
assert!(field.is_i64());
assert!(!field.is::<&i64>());Sourcepub fn is_i128(&self) -> bool
pub fn is_i128(&self) -> bool
Returns true if the field value is of type i128.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: i128,
}
let foo = Foo { a: i128::default() };
let field = foo.field("a").unwrap();
assert!(field.is_i128());
assert!(!field.is::<&i128>());Sourcepub fn is_isize(&self) -> bool
pub fn is_isize(&self) -> bool
Returns true if the field value is of type isize.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: isize,
}
let foo = Foo { a: isize::default() };
let field = foo.field("a").unwrap();
assert!(field.is_isize());
assert!(!field.is::<&isize>());Sourcepub fn is_f32(&self) -> bool
pub fn is_f32(&self) -> bool
Returns true if the field value is of type f32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: f32,
}
let foo = Foo { a: f32::default() };
let field = foo.field("a").unwrap();
assert!(field.is_f32());
assert!(!field.is::<&f32>());Sourcepub fn is_f64(&self) -> bool
pub fn is_f64(&self) -> bool
Returns true if the field value is of type f64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess)]
struct Foo {
a: f64,
}
let foo = Foo { a: f64::default() };
let field = foo.field("a").unwrap();
assert!(field.is_f64());
assert!(!field.is::<&f64>());Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the field value as bool.
This method is guaranteed to return Some(_) if
.is_bool() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into bool.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: bool,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_bool().is_some());Sourcepub fn as_u8(&self) -> Option<u8>
pub fn as_u8(&self) -> Option<u8>
Returns the field value as u8.
This method is guaranteed to return Some(_) if
.is_u8() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into u8.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: u8,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_u8().is_some());Sourcepub fn as_u16(&self) -> Option<u16>
pub fn as_u16(&self) -> Option<u16>
Returns the field value as u16.
This method is guaranteed to return Some(_) if
.is_u16() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into u16.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: u16,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_u16().is_some());Sourcepub fn as_u32(&self) -> Option<u32>
pub fn as_u32(&self) -> Option<u32>
Returns the field value as u32.
This method is guaranteed to return Some(_) if
.is_u32() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into u32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: u32,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_u32().is_some());Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
Returns the field value as u64.
This method is guaranteed to return Some(_) if
.is_u64() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into u64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: u64,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_u64().is_some());Sourcepub fn as_u128(&self) -> Option<u128>
pub fn as_u128(&self) -> Option<u128>
Returns the field value as u128.
This method is guaranteed to return Some(_) if
.is_u128() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into u128.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: u128,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_u128().is_some());Sourcepub fn as_usize(&self) -> Option<usize>
pub fn as_usize(&self) -> Option<usize>
Returns the field value as usize.
This method is guaranteed to return Some(_) if
.is_usize() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into usize.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: usize,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_usize().is_some());Sourcepub fn as_i8(&self) -> Option<i8>
pub fn as_i8(&self) -> Option<i8>
Returns the field value as i8.
This method is guaranteed to return Some(_) if
.is_i8() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into i8.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: i8,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_i8().is_some());Sourcepub fn as_i16(&self) -> Option<i16>
pub fn as_i16(&self) -> Option<i16>
Returns the field value as i16.
This method is guaranteed to return Some(_) if
.is_i16() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into i16.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: i16,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_i16().is_some());Sourcepub fn as_i32(&self) -> Option<i32>
pub fn as_i32(&self) -> Option<i32>
Returns the field value as i32.
This method is guaranteed to return Some(_) if
.is_i32() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into i32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: i32,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_i32().is_some());Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
Returns the field value as i64.
This method is guaranteed to return Some(_) if
.is_i64() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into i64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: i64,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_i64().is_some());Sourcepub fn as_i128(&self) -> Option<i128>
pub fn as_i128(&self) -> Option<i128>
Returns the field value as i128.
This method is guaranteed to return Some(_) if
.is_i128() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into i128.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: i128,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_i128().is_some());Sourcepub fn as_isize(&self) -> Option<isize>
pub fn as_isize(&self) -> Option<isize>
Returns the field value as isize.
This method is guaranteed to return Some(_) if
.is_isize() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into isize.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: isize,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_isize().is_some());Sourcepub fn as_f32(&self) -> Option<f32>
pub fn as_f32(&self) -> Option<f32>
Returns the field value as f32.
This method is guaranteed to return Some(_) if
.is_f32() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into f32.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: f32,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_f32().is_some());Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the field value as f64.
This method is guaranteed to return Some(_) if
.is_f64() returns true.
It may also return Some(_) if it is possible to perform a lossless conversion of
the field’s value into f64.
§Example
use field_access::FieldAccess;
#[derive(FieldAccess, Default)]
struct Foo {
a: f64,
}
let mut foo = Foo::default();
let mut field = foo.field("a").unwrap();
assert!(field.as_f64().is_some());