Field

Struct Field 

Source
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>

Source

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

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

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

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

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

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][..]));
Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Trait Implementations§

Source§

impl<'a> AsRef<Field<'a>> for FieldMut<'a>

Source§

fn as_ref(&self) -> &Field<'a>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for Field<'a>

Source§

fn clone(&self) -> Field<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Field<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Field<'a>

§

impl<'a> !RefUnwindSafe for Field<'a>

§

impl<'a> !Send for Field<'a>

§

impl<'a> !Sync for Field<'a>

§

impl<'a> Unpin for Field<'a>

§

impl<'a> !UnwindSafe for Field<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.