Skip to main content

Canary

Struct Canary 

Source
pub struct Canary<const MAGIC: u32> { /* private fields */ }
Expand description

An embeddable structure guard.

To use fbl::Canary, choose a 4-byte guard value.

You can use the Canary::new() method to instantiate a Canary object. The compiler will infer the magic value from the type definition.

struct MyStruct {
    canary: fbl::Canary<{ fbl::magic(b"guar") }>,
    // ...
}

impl MyStruct {
    fn new() -> Self {
        MyStruct {
            canary: fbl::Canary::new(),
            // ...
        }
    }
}

The canary initializes itself with the guard value during construction and checks it during destruction (on Drop). You can also manually check the value during the lifetime of your object by calling the assert method.

If the value is not an ASCII string, you can directly use an integer literal as the const generic parameter.

struct MyStruct {
    canary: fbl::Canary<0x12345678>,
    // ...
}

Implementations§

Source§

impl<const MAGIC: u32> Canary<MAGIC>

Source

pub const fn new() -> Self

Create a new Canary with the specified magic value.

Source

pub fn assert(&self)

Assert that the value of magic is as expected.

§Panics

Panics if self.magic is not the expected value.

Source

pub fn valid(&self) -> bool

Some places have special handling of bad magic values. For these cases, simply return whether the magic is correct, and let them respond appropriately if not.

Trait Implementations§

Source§

impl<const MAGIC: u32> Default for Canary<MAGIC>

Source§

fn default() -> Self

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

impl<const MAGIC: u32> Drop for Canary<MAGIC>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<const MAGIC: u32> Freeze for Canary<MAGIC>

§

impl<const MAGIC: u32> RefUnwindSafe for Canary<MAGIC>

§

impl<const MAGIC: u32> Send for Canary<MAGIC>

§

impl<const MAGIC: u32> Sync for Canary<MAGIC>

§

impl<const MAGIC: u32> Unpin for Canary<MAGIC>

§

impl<const MAGIC: u32> UnsafeUnpin for Canary<MAGIC>

§

impl<const MAGIC: u32> UnwindSafe for Canary<MAGIC>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. 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.