pub struct Obstack { /* private fields */ }
Expand description
An obstack
Implementations§
Source§impl Obstack
impl Obstack
Sourcepub fn with_initial_capacity(initial_capacity: usize) -> Self
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.
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new Obstack
.
The initial capacity will be set to DEFAULT_INITIAL_CAPACITY
.
Sourcepub fn push<'a, T>(&'a self, value: T) -> Ref<'a, T>
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!");
Sourcepub fn push_copy<'a, T>(&'a self, value: T) -> &'a mut Twhere
T: Copy,
pub fn push_copy<'a, T>(&'a self, value: T) -> &'a mut Twhere
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]);
Sourcepub fn copy_from_slice<'a, T>(&'a self, src: &[T]) -> &'a mut [T]where
T: Copy,
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][..]);
Sourcepub fn bytes_used(&self) -> usize
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);