Struct stack_any::StackAny

source ·
pub struct StackAny<const N: usize> { /* private fields */ }
Expand description

A convertible type that owns a stack allocation of N size.

Implementations§

source§

impl<const N: usize> StackAny<N>

source

pub fn new<T>(value: T) -> Self
where T: Any,

Allocates N-size memory on the stack and then places value into it.

§Panics

Panics if T size is larger than N.

§Examples
let five = stack_any::StackAny::<{ std::mem::size_of::<i32>() }>::new(5);
source

pub fn try_new<T>(value: T) -> Option<Self>
where T: Any,

Allocates N-size memory on the stack and then places value into it. Returns None if T size is larger than N.

§Examples
let five = stack_any::StackAny::<{ std::mem::size_of::<i32>() }>::try_new(5);
source

pub fn downcast_ref<T>(&self) -> &T
where T: Any,

Attempt to return reference to the inner value as a concrete type.

§Panics

Panics if T size is larger than N.

§Examples
let five = stack_any::stack_any!(i32, 5);
assert_eq!(five.downcast_ref::<i32>(), &5);
source

pub fn try_downcast_ref<T>(&self) -> Option<&T>
where T: Any,

Attempt to return reference to the inner value as a concrete type. Returns None if T size is larger than N.

§Examples
let five = stack_any::stack_any!(i32, 5);
assert_eq!(five.try_downcast_ref::<i32>(), Some(&5));
assert_eq!(five.try_downcast_ref::<i64>(), None);
source

pub fn downcast_mut<T>(&mut self) -> &mut T
where T: Any,

Attempt to return mutable reference to the inner value as a concrete type.

§Panics

Panics if T size is larger than N.

§Examples
let mut five = stack_any::stack_any!(i32, 5);
assert_eq!(five.downcast_mut::<i32>(), &mut 5);
source

pub fn try_downcast_mut<T>(&mut self) -> Option<&mut T>
where T: Any,

Attempt to return mutable reference to the inner value as a concrete type. Returns None if T size is larger than N.

§Examples
let mut five = stack_any::stack_any!(i32, 5);
assert_eq!(five.try_downcast_mut::<i32>(), Some(&mut 5));
assert_eq!(five.try_downcast_mut::<i64>(), None);
source

pub fn downcast<T>(self) -> T
where T: Any,

Attempt to downcast the stack to a concrete type.

§Panics

Panics if T size is larger than N.

§Examples
let five = stack_any::stack_any!(i32, 5);
assert_eq!(five.downcast::<i32>(), 5);
source

pub fn try_downcast<T>(self) -> Option<T>
where T: Any,

Attempt to downcast the stack to a concrete type. Returns None if T size is larger than N.

§Examples
let five = stack_any::stack_any!(i32, 5);
assert_eq!(five.try_downcast::<i32>(), Some(5));

Trait Implementations§

source§

impl<const N: usize> Debug for StackAny<N>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for StackAny<N>

§

impl<const N: usize> RefUnwindSafe for StackAny<N>

§

impl<const N: usize> Send for StackAny<N>

§

impl<const N: usize> Sync for StackAny<N>

§

impl<const N: usize> Unpin for StackAny<N>

§

impl<const N: usize> UnwindSafe for StackAny<N>

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

§

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

§

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.