pub struct MeteredVec<T> { /* private fields */ }Expand description
A Vec whose every growth is charged to a Meter before it happens
(its capacity, at heap_cost). Parsers build every table this way, so
a table can never hold more than the meter granted, however many records
the input declares; Self::into_parts hands the charge on to whatever
keeps the table.
Implementations§
Source§impl<T> MeteredVec<T>
impl<T> MeteredVec<T>
Sourcepub fn with_capacity(meter: &Arc<Meter>, n: usize) -> Result<Self, ChargeError>
pub fn with_capacity(meter: &Arc<Meter>, n: usize) -> Result<Self, ChargeError>
An empty vector with room for exactly n, charged first.
Sourcepub fn from_vec(vec: Vec<T>, meter: &Arc<Meter>) -> Result<Self, ChargeError>
pub fn from_vec(vec: Vec<T>, meter: &Arc<Meter>) -> Result<Self, ChargeError>
Wraps a vector the caller already allocated (a TPK’s rows, a test’s),
charging its capacity now. Parsers of input never use this: they grow
a MeteredVec from empty.
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element, retaining the charged capacity for reuse.
Sourcepub fn from_charged_parts(
vec: Vec<T>,
charge: Charge,
) -> Result<Self, ChargeError>
pub fn from_charged_parts( vec: Vec<T>, charge: Charge, ) -> Result<Self, ChargeError>
Transfer a buffer and its existing charge without allocating or copying.
Used by charged encoders. Any capacity not covered by the supplied
charge is booked before accepting ownership, as with from_vec.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
The elements, mutably (the length cannot grow through this).
Sourcepub fn reserve(&mut self, additional: usize) -> Result<(), ChargeError>
pub fn reserve(&mut self, additional: usize) -> Result<(), ChargeError>
Room for additional more, growing geometrically; charged first.
Sourcepub fn reserve_exact(&mut self, additional: usize) -> Result<(), ChargeError>
pub fn reserve_exact(&mut self, additional: usize) -> Result<(), ChargeError>
Room for exactly additional more; charged first.
Sourcepub fn push(&mut self, value: T) -> Result<(), ChargeError>
pub fn push(&mut self, value: T) -> Result<(), ChargeError>
Appends value, charging any growth first.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Cuts to len elements; the allocation and its charge stay.
Sourcepub fn into_parts(self) -> (Vec<T>, Charge)
pub fn into_parts(self) -> (Vec<T>, Charge)
The vector and its charge, which must live as long as the vector’s allocation does.
Sourcepub fn shrink_to_fit(&mut self) -> Result<(), ChargeError>
pub fn shrink_to_fit(&mut self) -> Result<(), ChargeError>
Gives back any spare capacity, the charge following it: shrunk in
place, its charge cut after (no second block exists, so nothing more
is charged); or, for a block that may be mapped given back below
128 KiB (super::shrink_moves; review round 13), moved into an
exact allocation charged before it is made (both blocks exist while
it moves), so a refusal leaves the vector as it was and nothing
allocated beyond what was charged.
Sourcepub fn into_boxed_slice(self) -> Result<(Box<[T]>, Charge), ChargeError>
pub fn into_boxed_slice(self) -> Result<(Box<[T]>, Charge), ChargeError>
The elements as a boxed slice, and its charge, with no spare
capacity (Self::shrink_to_fit).
Source§impl<T: Clone> MeteredVec<T>
impl<T: Clone> MeteredVec<T>
Sourcepub fn extend_from_slice(&mut self, items: &[T]) -> Result<(), ChargeError>
pub fn extend_from_slice(&mut self, items: &[T]) -> Result<(), ChargeError>
Appends a copy of items, charging any growth first.
Source§impl MeteredVec<Value>
impl MeteredVec<Value>
Sourcepub fn push_built(&mut self, item: Built) -> Result<(), ChargeError>
pub fn push_built(&mut self, item: Built) -> Result<(), ChargeError>
Appends a built value (a list’s or a tuple’s item); this vector keeps its charge from then on. A refused growth drops both.
Source§impl MeteredVec<(NodeId, Value)>
impl MeteredVec<(NodeId, Value)>
Sourcepub fn push_entry(
&mut self,
key: NodeId,
item: Built,
) -> Result<(), ChargeError>
pub fn push_entry( &mut self, key: NodeId, item: Built, ) -> Result<(), ChargeError>
Appends a dict entry whose value was built; this vector keeps its charge from then on. A refused growth drops both.
Sourcepub fn set_entry(&mut self, pos: usize, item: Built) -> bool
pub fn set_entry(&mut self, pos: usize, item: Built) -> bool
Replaces the value of entry pos with a built one, keeping its key
and position (a repeated name’s PyDict_SetItem, plan 3.10’s linear
dict), and keeps its charge. The value replaced is dropped; its charge
stays with this vector until it drops (charged, never under). False,
with item dropped whole, when pos is not an entry.