Struct agb::sync::Static

source ·
pub struct Static<T> { /* private fields */ }
Expand description

A helper that implements static variables.

It ensures that even if you use the same static variable in both an IRQ and normal code, the IRQ will never observe an invalid value of the variable.

This type only works with owned values. If you need to work with borrows, consider using sync::Mutex instead.

Performance

Writing or reading from a static variable is efficient under the following conditions:

  • The type is aligned to 4 bytes and can be stored in 40 bytes or less.
  • The type is aligned to 2 bytes and can be stored in 2 bytes.
  • The type is can be stored in a single byte.

Replacing the current value of the static variable is efficient under the following conditions:

  • The type is aligned to 4 bytes and can be stored in 4 bytes or less.
  • The type is can be stored in a single byte.

When these conditions are not met, static variables are handled using a fallback routine that disables IRQs and does a normal copy. This can be dangerous as disabling IRQs can cause your program to miss out on important interrupts such as V-Blank.

Consider using sync::Mutex instead if you need to use a large amount of operations that would cause IRQs to be disabled. Also consider using #[repr(align(4))] to force proper alignment for your type.

Implementations§

source§

impl<T> Static<T>

source

pub const fn new(val: T) -> Self

Creates a new static variable.

Examples found in repository?
examples/output.rs (line 6)
6
static COUNT: Static<u32> = Static::new(0);
source

pub fn replace(&self, val: T) -> T

Replaces the current value of the static variable with another, and returns the old value.

source

pub fn into_inner(self) -> T

Extracts the interior value of the static variable.

source§

impl<T: Copy> Static<T>

source

pub fn write(&self, val: T)

Writes a new value into this static variable.

source

pub fn read(&self) -> T

Reads a value from this static variable.

Trait Implementations§

source§

impl<T: Default> Default for Static<T>

source§

fn default() -> Self

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

impl<T> Send for Static<T>

source§

impl<T> Sync for Static<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Static<T>

§

impl<T> Unpin for Static<T>where T: Unpin,

§

impl<T> UnwindSafe for Static<T>where T: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.