Struct Obstack

Source
pub struct Obstack { /* private fields */ }
Expand description

An obstack

Implementations§

Source§

impl Obstack

Source

pub fn with_initial_capacity(initial_capacity: usize) -> Self

Constructs a new Obstack with the specified initial capacity.

The obstack will be able to allocate at least initial_capacity bytes before having to allocate again.

Source

pub fn new() -> Self

Constructs a new Obstack.

The initial capacity will be set to DEFAULT_INITIAL_CAPACITY.

Source

pub fn push<'a, T>(&'a self, value: T) -> Ref<'a, T>

Pushes a value to the Obstack.

Returns a Ref that can be dereferenced to the value’s location on the stack.

let r: Ref<String> = stack.push(String::from("Hello World!"));
assert_eq!(*r, "Hello World!");
Source

pub fn push_copy<'a, T>(&'a self, value: T) -> &'a mut T
where T: Copy,

Pushes a Copy value to the Obstack.

Returns a mutable reference to the value on the stack.

let r: &mut [u8; 5] = stack.push_copy([1,2,3,4,5]);
assert_eq!(*r, [1,2,3,4,5]);
Source

pub fn copy_from_slice<'a, T>(&'a self, src: &[T]) -> &'a mut [T]
where T: Copy,

Copies values from a slice to the Obstack.

Returns a mutable reference to a newly allocated slice:

let v: Box<[u8]> = Box::new([1,2,3,4,5]);
let r: &mut [u8] = stack.copy_from_slice(&v[0..3]);

assert_eq!(r.len(), 3);
assert_eq!(r, &[1,2,3][..]);

Zero-length slices work as expected without allocations:

let prev_used = stack.bytes_used();
let r: &mut [u8] = stack.copy_from_slice(&[]);

assert_eq!(prev_used, stack.bytes_used());
assert_eq!(r.len(), 0);
assert_eq!(r, &[]);

As do slices of zero-sized types:

let v: Box<[()]> = Box::new([(); 1_000]);
let prev_used = stack.bytes_used();
let r: &mut [()] = stack.copy_from_slice(&v);

assert_eq!(prev_used, stack.bytes_used());
assert_eq!(r.len(), 1_000);
assert!(r == &[(); 1_000][..]);
Source

pub fn bytes_used(&self) -> usize

Returns the total bytes currently used by values.

Includes bytes used for alignment padding. However, this figure is not the total size allocated by the Obstack, which would include bytes allocated for parts of segments that haven’t been used yet. Thus the return value of this method will change after every non-zero-sized value allocated.

bytes_used always starts at zero:

let stack = Obstack::new();
assert_eq!(stack.bytes_used(), 0);

Trait Implementations§

Source§

impl Debug for Obstack

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for Obstack

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.