[−][src]Struct abi_stable::sabi_types::rsmallbox::RSmallBox
A box type which stores small values inline as an optimization.
Inline storage
The Inline
type parameter
is the storage space on the stack (as in inline with the RSmallBox
struct)
where small values get stored,instead of storing them on the heap.
It has to have an alignment greater than or equal to the value being stored, otherwise storing the value on the heap.
To ensure that the inline storage has enough alignemnt you can use one of the
AlignTo*
types from the (reexported) alignment submodule,
or from abi_stable::inline_storage::alignment
.
Examples
In a nonexhaustive enum
Using an RSmallBox to store a generic type in a nonexhaustive enum.
use abi_stable::{ sabi_types::RSmallBox, std_types::RString, reexports::SelfOps, StableAbi, }; #[repr(u8)] #[derive(StableAbi,Debug,Clone,PartialEq)] #[sabi(kind(WithNonExhaustive( // Determines the maximum size of this enum in semver compatible versions. // This is 7 usize large because: // - The enum discriminant occupies 1 usize(because the enum is usize aligned). // - RSmallBox<T,[usize;4]>: is 6 usize large size="[usize;7]", // Determines the traits that are required when wrapping this enum in NonExhaustive, // and are then available with it. traits(Debug,Clone,PartialEq), )))] pub enum SomeEnum<T>{ #[doc(hidden)] __NonExhaustive, Foo, Bar, // This variant was added in a newer (compatible) version of the library. Baz(RSmallBox<T,[usize;4]>) } impl<T> SomeEnum<T>{ pub fn is_inline(&self)->bool{ match self { SomeEnum::__NonExhaustive=>true, SomeEnum::Foo=>true, SomeEnum::Bar=>true, SomeEnum::Baz(rsbox)=>RSmallBox::is_inline(rsbox), } } pub fn is_heap_allocated(&self)->bool{ !self.is_inline() } } #[repr(C)] #[derive(StableAbi,Debug,Clone,PartialEq)] pub struct FullName{ pub name:RString, pub surname:RString, } let rstring="Oh boy!" .piped(RString::from) .piped(RSmallBox::new) .piped(SomeEnum::Baz); let full_name= FullName{ name:"R__e".into(), surname:"L_____e".into() } .piped(RSmallBox::new) .piped(SomeEnum::Baz); assert!( rstring.is_inline() ); assert!( full_name.is_heap_allocated() );
Trying out different Inline
type parameters
This example demonstrates how changing the Inline
type parameter can
change whether an RString is stored inline or on the heap.
use abi_stable::{ inline_storage::alignment::AlignToUsize, sabi_types::RSmallBox, std_types::RString, StableAbi, }; use std::mem; type JustRightInlineBox<T>= RSmallBox<T,AlignToUsize<[u8; mem::size_of::<usize>()*4 ]>>; let string=RString::from("What is that supposed to mean?"); let small=RSmallBox::<_,[usize;3]>::new(string.clone()); let medium=RSmallBox::<_,[usize;4]>::new(string.clone()); let large=RSmallBox::<_,[usize;16]>::new(string.clone()); let not_enough_alignment=RSmallBox::<_,[u8;64]>::new(string.clone()); let just_right=JustRightInlineBox::new(string.clone()); assert!( RSmallBox::is_heap_allocated(&small) ); assert!( RSmallBox::is_inline(&medium) ); assert!( RSmallBox::is_inline(&large) ); assert!( RSmallBox::is_heap_allocated(¬_enough_alignment) ); assert!( RSmallBox::is_inline(&just_right) );
Implementations
impl<T, Inline> RSmallBox<T, Inline>
[src]
pub fn new(value: T) -> RSmallBox<T, Inline> where
Inline: InlineStorage,
[src]
Inline: InlineStorage,
Constructs this RSmallBox from a value.
Example
use abi_stable::{ sabi_types::RSmallBox, std_types::RString, }; let xbox=RSmallBox::<_,[usize;4]>::new(RString::from("one"));
pub fn as_mut_ptr(this: &mut Self) -> *mut T
[src]
Gets a raw pointer into the underlying data.
Example
use abi_stable::{ sabi_types::RSmallBox, std_types::RString, }; let mut play=RSmallBox::<_,[usize;4]>::new(RString::from("station")); let play_addr=&mut play as *mut RSmallBox<_,_> as usize; let heap_addr=RSmallBox::as_mut_ptr(&mut play) as usize; assert_eq!(play_addr,heap_addr);
pub fn as_ptr(this: &Self) -> *const T
[src]
Gets a raw pointer into the underlying data.
Example
use abi_stable::{ sabi_types::RSmallBox, reexports::SelfOps, std_types::RVec, }; let mut generations=vec![1,2,3,4,5,6,7,8] .piped(RVec::from) .piped(RSmallBox::<_,[usize;2]>::new); let generations_addr=&generations as *const RSmallBox<_,_> as usize; let heap_addr=RSmallBox::as_ptr(&generations) as usize; assert_ne!(generations_addr,heap_addr);
pub fn from_move_ptr(from_ptr: MovePtr<'_, T>) -> Self where
Inline: InlineStorage,
[src]
Inline: InlineStorage,
Constructs this RSmallBox
from a MovePtr
.
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::RSmallBox, std_types::RBox, }; let rbox=RBox::new(1000_u64); let rsbox:RSmallBox<u64,[u64;1]>= rbox.in_move_ptr(|x| RSmallBox::<u64,[u64;1]>::from_move_ptr(x) ); assert_eq!( *rsbox, 1000_u64 );
pub fn move_<Inline2>(this: Self) -> RSmallBox<T, Inline2> where
Inline2: InlineStorage,
[src]
Inline2: InlineStorage,
Converts this RSmallBox
into another one with a differnet inline size.
Example
use abi_stable::sabi_types::RSmallBox; let old=RSmallBox::<u64,[u8;4]>::new(599_u64); assert!( ! RSmallBox::is_inline(&old) ); let new=RSmallBox::move_::<[u64;1]>(old); assert!( RSmallBox::is_inline(&new) ); assert_eq!(*new,599_u64);
pub fn is_inline(this: &Self) -> bool
[src]
Queries whether the value is stored inline.
Example
use abi_stable::{ sabi_types::RSmallBox, std_types::RString, }; let heap=RSmallBox::<u64,[u8;4]>::new(599_u64); assert!( ! RSmallBox::is_inline(&heap) ); let inline=RSmallBox::<RString,[usize;4]>::new("hello".into()); assert!( RSmallBox::is_inline(&inline) );
pub fn is_heap_allocated(this: &Self) -> bool
[src]
Queries whether the value is stored on the heap.
Example
use abi_stable::{ sabi_types::RSmallBox, std_types::RHashMap, }; let heap=RSmallBox::<_,[u8;4]>::new(String::new()); assert!( RSmallBox::is_heap_allocated(&heap) ); let inline=RSmallBox::<_,[usize;3]>::new(RHashMap::<u8,()>::new()); assert!( ! RSmallBox::is_heap_allocated(&inline) );
pub fn into_inner(this: Self) -> T
[src]
Unwraps this pointer into its owned value.
Example
use abi_stable::sabi_types::RSmallBox; let rbox=RSmallBox::<_,[usize;3]>::new(vec![0,1,2]); assert_eq!( RSmallBox::into_inner(rbox), vec![0,1,2] );
Trait Implementations
impl<T, O, Inline> CanTransmuteElement<O> for RSmallBox<T, Inline>
[src]
type TransmutedPtr = RSmallBox<O, Inline>
The type of the pointer after it's element type has been changed.
impl<T, Inline> Clone for RSmallBox<T, Inline> where
T: Clone,
Inline: InlineStorage,
[src]
T: Clone,
Inline: InlineStorage,
pub fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T, Inline> Debug for RSmallBox<T, Inline> where
T: Debug,
Inline: Debug,
[src]
T: Debug,
Inline: Debug,
impl<T, Inline> Default for RSmallBox<T, Inline> where
T: Default,
Inline: InlineStorage,
[src]
T: Default,
Inline: InlineStorage,
impl<T, Inline> Deref for RSmallBox<T, Inline>
[src]
impl<T, Inline> DerefMut for RSmallBox<T, Inline>
[src]
impl<'de, T, Inline> Deserialize<'de> for RSmallBox<T, Inline> where
Inline: InlineStorage,
T: Deserialize<'de>,
[src]
Inline: InlineStorage,
T: Deserialize<'de>,
pub fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
impl<T, Inline> Display for RSmallBox<T, Inline> where
T: Display,
[src]
T: Display,
impl<T, Inline> Drop for RSmallBox<T, Inline>
[src]
impl<T, Inline> Eq for RSmallBox<T, Inline> where
T: Eq,
Inline: Eq,
[src]
T: Eq,
Inline: Eq,
impl<T, Inline> From<RBox<T>> for RSmallBox<T, Inline> where
Inline: InlineStorage,
[src]
Inline: InlineStorage,
Converts an RBox into an RSmallBox,currently this allocates.
impl<T, Inline> GetPointerKind for RSmallBox<T, Inline>
[src]
impl<T, Inline> GetStaticEquivalent_ for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
[src]
T: __StableAbi,
Inline: __GetStaticEquivalent_,
type StaticEquivalent = _static_RSmallBox<__GetStaticEquivalent<T>, __GetStaticEquivalent<Inline>>
impl<T, Inline> Hash for RSmallBox<T, Inline> where
T: Hash,
Inline: Hash,
[src]
T: Hash,
Inline: Hash,
pub fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
H: Hasher,
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T, Inline> Into<RBox<T>> for RSmallBox<T, Inline> where
Inline: InlineStorage,
[src]
Inline: InlineStorage,
Converts a RSmallBox into an RBox,currently this allocates.
impl<T, Inline> Ord for RSmallBox<T, Inline> where
T: Ord,
Inline: Ord,
[src]
T: Ord,
Inline: Ord,
pub fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T, Inline> OwnedPointer for RSmallBox<T, Inline>
[src]
pub unsafe fn get_move_ptr(
this: &mut ManuallyDrop<Self>
) -> MovePtr<'_, Self::Target>
[src]
this: &mut ManuallyDrop<Self>
) -> MovePtr<'_, Self::Target>
pub unsafe fn drop_allocation(this: &mut ManuallyDrop<Self>)
[src]
pub fn with_move_ptr<F, R>(mut this: ManuallyDrop<Self>, f: F) -> R where
F: FnOnce(MovePtr<'_, Self::Target>) -> R,
Self::Target: Sized,
[src]
F: FnOnce(MovePtr<'_, Self::Target>) -> R,
Self::Target: Sized,
pub fn in_move_ptr<F, R>(self, f: F) -> R where
F: FnOnce(MovePtr<'_, Self::Target>) -> R,
Self::Target: Sized,
[src]
F: FnOnce(MovePtr<'_, Self::Target>) -> R,
Self::Target: Sized,
impl<T, Inline> PartialEq<RSmallBox<T, Inline>> for RSmallBox<T, Inline> where
T: PartialEq,
Inline: PartialEq,
[src]
T: PartialEq,
Inline: PartialEq,
pub fn eq(&self, other: &Self) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T, Inline> PartialOrd<RSmallBox<T, Inline>> for RSmallBox<T, Inline> where
T: PartialOrd,
Inline: PartialOrd,
[src]
T: PartialOrd,
Inline: PartialOrd,
pub fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: Send, Inline> Send for RSmallBox<T, Inline>
[src]
impl<T, Inline> Serialize for RSmallBox<T, Inline> where
T: Serialize,
[src]
T: Serialize,
impl<T, Inline> StableAbi for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
[src]
T: __StableAbi,
Inline: __GetStaticEquivalent_,
type IsNonZeroType = False
Whether this type has a single invalid bit-pattern. Read more
pub const LAYOUT: &'static TypeLayout
[src]
pub const ABI_CONSTS: AbiConsts
[src]
impl<T: Sync, Inline> Sync for RSmallBox<T, Inline>
[src]
Auto Trait Implementations
impl<T, Inline> RefUnwindSafe for RSmallBox<T, Inline> where
Inline: RefUnwindSafe,
T: RefUnwindSafe,
[src]
Inline: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, Inline> Unpin for RSmallBox<T, Inline> where
Inline: Unpin,
T: Unpin,
[src]
Inline: Unpin,
T: Unpin,
impl<T, Inline> UnwindSafe for RSmallBox<T, Inline> where
Inline: UnwindSafe,
T: RefUnwindSafe + UnwindSafe,
[src]
Inline: UnwindSafe,
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<'a, T> BorrowOwned<'a> for T where
T: 'a + Clone,
[src]
T: 'a + Clone,
type ROwned = T
The owned type, stored in RCow::Owned
type RBorrowed = &'a T
The borrowed type, stored in RCow::Borrowed
pub fn r_borrow(
&'a <T as BorrowOwned<'a>>::ROwned
) -> <T as BorrowOwned<'a>>::RBorrowed
[src]
&'a <T as BorrowOwned<'a>>::ROwned
) -> <T as BorrowOwned<'a>>::RBorrowed
pub fn r_to_owned(
<T as BorrowOwned<'a>>::RBorrowed
) -> <T as BorrowOwned<'a>>::ROwned
[src]
<T as BorrowOwned<'a>>::RBorrowed
) -> <T as BorrowOwned<'a>>::ROwned
pub fn deref_borrowed(&<T as BorrowOwned<'a>>::RBorrowed) -> &T
[src]
pub fn deref_owned(&<T as BorrowOwned<'a>>::ROwned) -> &T
[src]
pub fn from_cow_borrow(&'a T) -> <T as BorrowOwned<'a>>::RBorrowed
[src]
pub fn from_cow_owned(<T as ToOwned>::Owned) -> <T as BorrowOwned<'a>>::ROwned
[src]
pub fn into_cow_borrow(<T as BorrowOwned<'a>>::RBorrowed) -> &'a T
[src]
pub fn into_cow_owned(<T as BorrowOwned<'a>>::ROwned) -> <T as ToOwned>::Owned
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T> GetWithMetadata for T
[src]
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> SelfOps for T where
T: ?Sized,
[src]
T: ?Sized,
pub const T: PhantomData<fn() -> Self>
[src]
pub const T_D: PhantomData<Self>
[src]
pub fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Self
[src]
pub fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Self
[src]
pub fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Self
[src]
pub fn ty_(&self) -> PhantomData<fn() -> Self>
[src]
pub fn ty_d(&self) -> PhantomData<Self>
[src]
pub fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
[src]
pub fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
[src]
pub fn eq_id(&self, other: &Self) -> bool
[src]
pub fn piped<F, U>(self, f: F) -> U where
F: FnOnce(Self) -> U,
[src]
F: FnOnce(Self) -> U,
pub fn piped_ref<'a, F, U>(&'a self, f: F) -> U where
F: FnOnce(&'a Self) -> U,
[src]
F: FnOnce(&'a Self) -> U,
pub fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U where
F: FnOnce(&'a mut Self) -> U,
[src]
F: FnOnce(&'a mut Self) -> U,
pub fn mutated<F>(self, f: F) -> Self where
F: FnOnce(&mut Self),
[src]
F: FnOnce(&mut Self),
pub fn observe<F>(self, f: F) -> Self where
F: FnOnce(&Self),
[src]
F: FnOnce(&Self),
pub fn into_<T>(self, PhantomData<fn() -> T>) -> T where
Self: Into<T>,
[src]
Self: Into<T>,
pub fn as_ref_<T>(&self) -> &T where
Self: AsRef<T>,
T: ?Sized,
[src]
Self: AsRef<T>,
T: ?Sized,
pub fn as_mut_<T>(&mut self) -> &mut T where
Self: AsMut<T>,
T: ?Sized,
[src]
Self: AsMut<T>,
T: ?Sized,
pub fn drop_(self)
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<This> TransmuteElement for This where
This: ?Sized,
[src]
This: ?Sized,
pub unsafe fn transmute_element<T>(self) -> Self::TransmutedPtr where
Self: CanTransmuteElement<T>,
Self::Target: Sized,
[src]
Self: CanTransmuteElement<T>,
Self::Target: Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The error type returned when the conversion fails.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> TypeIdentity for T where
T: ?Sized,
[src]
T: ?Sized,
type Type = T
The same type as Self. Read more
pub fn into_type_val(self) -> Self::Type where
Self::Type: Sized,
[src]
Self::Type: Sized,
pub fn into_type_ref(&self) -> &Self::Type
[src]
pub fn into_type_mut(&mut self) -> &mut Self::Type
[src]
pub fn into_type_box(self: Box<Self, Global>) -> Box<Self::Type, Global>
[src]
pub fn into_type_arc(this: Arc<Self>) -> Arc<Self::Type>
[src]
pub fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>
[src]
pub fn from_type_val(this: Self::Type) -> Self where
Self::Type: Sized,
[src]
Self::Type: Sized,
pub fn from_type_ref(this: &Self::Type) -> &Self
[src]
pub fn from_type_mut(this: &mut Self::Type) -> &mut Self
[src]
pub fn from_type_box(this: Box<Self::Type, Global>) -> Box<Self, Global>
[src]
pub fn from_type_arc(this: Arc<Self::Type>) -> Arc<Self>
[src]
pub fn from_type_rc(this: Rc<Self::Type>) -> Rc<Self>
[src]
impl<This> ValidTag_Bounds for This where
This: Debug + Clone + PartialEq<This>,
[src]
This: Debug + Clone + PartialEq<This>,