Struct abi_stable::sabi_types::rsmallbox::RSmallBox [−][src]
#[repr(C)]pub struct RSmallBox<T, Inline> { /* fields omitted */ }
Expand description
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::{reexports::SelfOps, sabi_types::RSmallBox, std_types::RString, 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
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"));
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);
Gets a raw pointer into the underlying data.
Example
use abi_stable::{reexports::SelfOps, sabi_types::RSmallBox, 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);
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);
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);
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));
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));
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
type TransmutedPtr = RSmallBox<O, Inline>
type TransmutedPtr = RSmallBox<O, Inline>
The type of the pointer after it’s element type has been changed.
Transmutes the element type of this pointer.. Read more
impl<'de, T, Inline> Deserialize<'de> for RSmallBox<T, Inline> where
Inline: InlineStorage,
T: Deserialize<'de>,
impl<'de, T, Inline> Deserialize<'de> for RSmallBox<T, Inline> where
Inline: InlineStorage,
T: Deserialize<'de>,
Deserialize this value from the given Serde deserializer. Read more
Converts an RBox into an RSmallBox,currently this allocates.
Converts a RSmallBox into an RBox,currently this allocates.
type Kind = PK_SmartPointer
type Kind = PK_SmartPointer
The kind of the pointer. Read more
impl<T, Inline> GetStaticEquivalent_ for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
impl<T, Inline> GetStaticEquivalent_ for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
type StaticEquivalent = _static_RSmallBox<__GetStaticEquivalent<T>, __GetStaticEquivalent<Inline>>
Gets a move pointer to the contents of this pointer. Read more
Deallocates the pointer without dropping its owned contents. Read more
fn with_move_ptr<F, R>(this: ManuallyDrop<Self>, func: F) -> R where
F: FnOnce(MovePtr<'_, Self::PtrTarget>) -> R,
fn with_move_ptr<F, R>(this: ManuallyDrop<Self>, func: F) -> R where
F: FnOnce(MovePtr<'_, Self::PtrTarget>) -> R,
Runs a callback with the contents of this pointer, and then deallocates it. Read more
impl<T, Inline> PartialOrd<RSmallBox<T, Inline>> for RSmallBox<T, Inline> where
T: PartialOrd,
Inline: PartialOrd,
impl<T, Inline> PartialOrd<RSmallBox<T, Inline>> for RSmallBox<T, Inline> where
T: PartialOrd,
Inline: PartialOrd,
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
impl<T, Inline> StableAbi for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
impl<T, Inline> StableAbi for RSmallBox<T, Inline> where
T: __StableAbi,
Inline: __GetStaticEquivalent_,
type IsNonZeroType = False
type IsNonZeroType = False
Whether this type has a single invalid bit-pattern. Read more
The layout of the type provided by implementors.
const
-equivalents of the associated types.
Auto Trait Implementations
impl<T, Inline> RefUnwindSafe for RSmallBox<T, Inline> where
Inline: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, Inline> UnwindSafe for RSmallBox<T, Inline> where
Inline: UnwindSafe,
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
type ROwned = T
type ROwned = T
The owned type, stored in RCow::Owned
type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
Compares the address of self
with the address of other
. Read more
Emulates the pipeline operator, allowing method syntax in more places. Read more
The same as piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read more
The same as piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
. Read more
Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more
Observes the value of self, passing it along unmodified. Useful in long method chains. Read more
Performs a conversion with Into
.
using the turbofish .into_::<_>()
syntax. Read more
Performs a reference to reference conversion with AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more
Performs a mutable reference to mutable reference conversion with AsMut
,
using the turbofish .as_mut_::<_>()
syntax. Read more
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
Transmutes the element type of this pointer.. Read more
type Type = T
type Type = T
This is always Self
.
Converts a value back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
Converts a value back to the original type.
Converts a reference back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
alloc
only.Converts an Arc back to the original type.
alloc
only.Converts an Rc back to the original type.