pub struct VBox<T: VarLen>(_);
Expand description

Equivalent of Box<T> for variable-length types.

Examples

Heap-allocated Str:

use varlen::prelude::*;
let s = VBox::new(Str::copy("hello"));
assert_eq!("hello", &s[..]);

Implementations

Allocates memory which is right-sized for this particular instance.

Examples
use varlen::prelude::*;
let s = VBox::new(Str::copy("hello"));
assert_eq!("hello", &s[..]);

Mutable access to the field.

Examples
use varlen::prelude::*;
let mut s = VBox::new(Str::copy("Hello"));
assert_eq!("Hello", &s[..]);
s.as_mut().mut_slice().make_ascii_uppercase();
assert_eq!("HELLO", &s[..]);
s.as_mut().mut_slice().make_ascii_lowercase();
assert_eq!("hello", &s[..]);

Converts this to a raw pointer representation.

Safety

Because T is a variable-length type, there are additional safety obligations above and beyond the usual treatment of NonNull<T>. In particular, the caller responsible for ensuring that whenever a &T is produced, the header-specified layout matches the layout of the tail. This prohibits code patterns such as overwriting the header in a way that changes the layout.

Example

Safe roundtripping through a raw pointer:

use varlen::prelude::*;

let b = VBox::new(Str::copy("hello"));
let b = unsafe {
    let p = b.into_raw();
    VBox::from_raw(p)
};
assert_eq!(&b[..], "hello");

Constructs a VBox<T> from a NonNull<T> pointer.

Safety

The layout of T’s tail, which is the variable-sized part not included in std::mem::size_of::<T>(), must be consistent with the layout specified by T’s header. For example, this can have been produced by a crate::Initializer<T> call or similar, on a buffer sufficiently sized for the initializer’s layout.

Example

Safe roundtripping through a raw pointer:

use varlen::prelude::*;

let b = VBox::new(Str::copy("hello"));
let b = unsafe {
    let p = b.into_raw();
    VBox::from_raw(p)
};
assert_eq!("hello", &b[..]);

Trait Implementations

Cloning a VBox<T> uses T::vclone().

Examples

use varlen::prelude::*;
let str: VBox<Str> = VBox::new(Str::copy("hello"));
let str2 = str.clone();
assert_eq!(&str2[..], "hello");

See also

It is often better to use T::vclone() or vcopy() instead, which will create a lazy initializer that directly clones or copies into the destination.

use varlen::prelude::*;
let str: VBox<Str> = VBox::new(Str::copy("hello"));
let seq: Seq<Str> = seq![
    // Best. Calls memcpy straight into the sequence storage
    str.vcopy(),   
    // Ok. Does field-by-field copy into the sequence storage.
    str.vclone(),  
    // Worst. Allocates a temporary VBox<Str>, copies field-by-field to there,
    // then coies field-by-field to the sequence storage, then deallocates the
    // temporary.
    str.clone(),   
];
let mut iter = seq.iter();
assert_eq!(&iter.next().unwrap()[..], "hello");
assert_eq!(&iter.next().unwrap()[..], "hello");
assert_eq!(&iter.next().unwrap()[..], "hello");
assert!(iter.next().is_none());

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

The resulting type after dereferencing.

Dereferences the value.

Executes the destructor for this type. Read more

VBox<T> is an initializer for T.

Examples

Pushing a VBox<T> onto a crate::seq::Seq<T>:

use varlen::prelude::*;

let mut seq: Seq<Str> = Seq::new();
let b = VBox::new(Str::copy("hello"));
seq.push(b);

Populates the destination pointer. Read more

Calculates the layout of the object, returning None if any of the calculated sizes or offsets would overflow usize. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.