Fields

Struct Fields 

Source
pub struct Fields<R: RegisterValue, T = ()> { /* private fields */ }
Expand description

A set of register fields

§Created by:

  • Combining (with |) fields generated by the [periph!] or the [register!] macro.
  • Default or Fields::empty: a set with no fields.

§Usable with:

These fields be combined together with |, & and ^. For operators that returns the same type as their first operand, the “assign” form (i.e. |=, &= and ^=) can also be used.

§Example

use peripherals::{register, Fields, Value};

register! {
    Register: u8 = 0b1001 {
        BIT1: 0 = struct Bit1(bool);
        BIT2: 1 = struct Bit2(bool);
        TWO_BITS: 2..3 = struct TwoBits(u8);
    }
}

// Obtain it with the `empty` method or by combining fields
let empty = Fields::<Register>::empty();
assert_eq!(empty.mask(), 0b0000);
let bits_12 = Register::BIT1 | Register::BIT2;
assert_eq!(bits_12.mask(), 0b0011);
let fields = Register::BIT1 | Register::TWO_BITS;
assert_eq!(fields.mask(), 0b1101);

// Combine fields together
assert_eq!(bits_12 | fields, Register::BIT1 | Register::BIT2 | Register::TWO_BITS);
assert_eq!(bits_12 & fields, Register::BIT1);
assert_eq!(bits_12 ^ fields, Register::BIT2 | Register::TWO_BITS);

assert_eq!(bits_12 | empty, bits_12);
assert_eq!(bits_12 & empty, empty);
assert_eq!(bits_12 ^ empty, bits_12);

let mut value = Value::reset();
assert_eq!(value.value(), 0b1001);

// Use it to read fields
let value_12 = value & (Register::BIT1 | Register::BIT2);
assert_eq!(value_12.bits(), 0b001);
assert_eq!((value & Register::TWO_BITS).bits(), 0b1000);

// Toggle single-bit fields
value ^= bits_12;
assert_eq!(value.value(), 0b1010);
value ^= Register::BIT1;
assert_eq!(value.value(), 0b1011);

// Write back bits previously read
value |= value_12;
assert_eq!(value.value(), 0b1001);

Implementations§

Source§

impl<R: RegisterValue, T> Fields<R, T>

Source

pub fn mask(self) -> R::Int

Get the raw mask

Source

pub unsafe fn from_raw(mask: R::Int) -> Fields<R, T>

Build from raw mask

§Safety

You should ensure the mask is valid for the fields of the associated register.

Source

pub fn empty() -> Fields<R, T>

An empty set with no fields

Trait Implementations§

Source§

impl<R: RegisterValue, T, U> BitAnd<T> for Fields<R, U>
where T: Either<U> + Into<Fields<R>>,

Source§

type Output = Fields<R, <T as Either<U>>::Output>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: T) -> Self::Output

Performs the & operation. Read more
Source§

impl<R: RegisterValue, T, U> BitAndAssign<T> for Fields<R, U>
where T: Either<U, Output = U> + Into<Fields<R>>,

Source§

fn bitand_assign(&mut self, other: T)

Performs the &= operation. Read more
Source§

impl<R: RegisterValue, T, U> BitOr<T> for Fields<R, U>
where T: Both<U> + Into<Fields<R>>,

Source§

type Output = Fields<R, <T as Both<U>>::Output>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: T) -> Self::Output

Performs the | operation. Read more
Source§

impl<R: RegisterValue, T, U> BitOrAssign<T> for Fields<R, U>
where T: Both<U, Output = U> + Into<Fields<R>>,

Source§

fn bitor_assign(&mut self, other: T)

Performs the |= operation. Read more
Source§

impl<R: RegisterValue, T, U> BitXor<T> for Fields<R, U>
where T: Both<U> + Into<Fields<R>>,

Source§

type Output = Fields<R, <T as Both<U>>::Output>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: T) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<R: RegisterValue, T, U> BitXorAssign<T> for Fields<R, U>
where T: Both<U, Output = U> + Into<Fields<R>>,

Source§

fn bitxor_assign(&mut self, other: T)

Performs the ^= operation. Read more
Source§

impl<R: RegisterValue, T> Clone for Fields<R, T>

Source§

fn clone(&self) -> Fields<R, T>

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<R: RegisterValue, T> Debug for Fields<R, T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<R: RegisterValue, T> Default for Fields<R, T>

Source§

fn default() -> Fields<R, T>

Returns the “default value” for a type. Read more
Source§

impl<R: RegisterValue, T> From<Field<R, T, <R as RegisterValue>::Int>> for Fields<R, ()>

Source§

fn from(field: Field<R, T, R::Int>) -> Fields<R, ()>

Converts to this type from the input type.
Source§

impl<R: RegisterValue, T: MayToggle<Toggle = Toggle>> From<Field<R, T, <R as RegisterValue>::Int>> for Fields<R, Toggle>

Source§

fn from(field: Field<R, T, R::Int>) -> Fields<R, Toggle>

Converts to this type from the input type.
Source§

impl<R: RegisterValue> From<Fields<R, Toggle>> for Fields<R, ()>

Source§

fn from(fields: Fields<R, Toggle>) -> Fields<R, ()>

Converts to this type from the input type.
Source§

impl<R: RegisterValue, T> MayToggle for Fields<R, T>

Source§

type Toggle = T

Toggle if it can be toggled, () otherwise
Source§

impl<R: RegisterValue, T: Into<Fields<R>> + Copy, U> PartialEq<T> for Fields<R, U>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<R: RegisterValue, T> Copy for Fields<R, T>

Auto Trait Implementations§

§

impl<R, T> Freeze for Fields<R, T>
where <R as RegisterValue>::Int: Freeze,

§

impl<R, T> RefUnwindSafe for Fields<R, T>

§

impl<R, T> Send for Fields<R, T>
where <R as RegisterValue>::Int: Send, R: Send, T: Send,

§

impl<R, T> Sync for Fields<R, T>
where <R as RegisterValue>::Int: Sync, R: Sync, T: Sync,

§

impl<R, T> Unpin for Fields<R, T>
where <R as RegisterValue>::Int: Unpin, R: Unpin, T: Unpin,

§

impl<R, T> UnwindSafe for Fields<R, T>

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, 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.