Struct prism_parser::parser::parser_instance::Arena
source · pub struct Arena<T> { /* private fields */ }Expand description
An arena of objects of type T.
§Example
use typed_arena::Arena;
struct Monster {
level: u32,
}
let monsters = Arena::new();
let vegeta = monsters.alloc(Monster { level: 9001 });
assert!(vegeta.level > 9000);Implementations§
source§impl<T> Arena<T>
impl<T> Arena<T>
sourcepub fn with_capacity(n: usize) -> Arena<T>
pub fn with_capacity(n: usize) -> Arena<T>
Construct a new arena with capacity for n values pre-allocated.
§Example
use typed_arena::Arena;
let arena = Arena::with_capacity(1337);sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the size of the arena
This is useful for using the size of previous typed arenas to build new typed arenas with large enough spaces.
§Example
use typed_arena::Arena;
let arena = Arena::with_capacity(0);
let a = arena.alloc(1);
let b = arena.alloc(2);
assert_eq!(arena.len(), 2);sourcepub fn alloc(&self, value: T) -> &mut T
pub fn alloc(&self, value: T) -> &mut T
Allocates a value in the arena, and returns a mutable reference to that value.
§Example
use typed_arena::Arena;
let arena = Arena::new();
let x = arena.alloc(42);
assert_eq!(*x, 42);sourcepub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]where
I: IntoIterator<Item = T>,
pub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]where
I: IntoIterator<Item = T>,
Uses the contents of an iterator to allocate values in the arena. Returns a mutable slice that contains these values.
§Example
use typed_arena::Arena;
let arena = Arena::new();
let abc = arena.alloc_extend("abcdefg".chars().take(3));
assert_eq!(abc, ['a', 'b', 'c']);sourcepub unsafe fn alloc_uninitialized(&self, num: usize) -> &mut [MaybeUninit<T>]
pub unsafe fn alloc_uninitialized(&self, num: usize) -> &mut [MaybeUninit<T>]
Allocates space for a given number of values, but doesn’t initialize it.
§Safety
After calling this method, the arena considers the elements initialized. If you fail to initialize them (which includes because of panicking during the initialization), the arena will run destructors on the uninitialized memory. Therefore, you must initialize them.
Considering how easy it is to cause undefined behaviour using this, you’re advised to
prefer the other (safe) methods, like alloc_extend.
§Example
use std::mem::{self, MaybeUninit};
use std::ptr;
use typed_arena::Arena;
// Transmute from MaybeUninit slice to slice of initialized T.
// It is a separate function to preserve the lifetime of the reference.
unsafe fn transmute_uninit<A>(r: &mut [MaybeUninit<A>]) -> &mut [A] {
mem::transmute(r)
}
let arena: Arena<bool> = Arena::new();
let slice: &mut [bool];
unsafe {
let uninitialized = arena.alloc_uninitialized(10);
for elem in uninitialized.iter_mut() {
ptr::write(elem.as_mut_ptr(), true);
}
slice = transmute_uninit(uninitialized);
}§Alternative allocation pattern
To avoid the problem of dropping assumed to be initialized elements on panic, it is also
possible to combine the reserve_extend with
uninitialized_array, initialize the elements and confirm
them by this method. In such case, when there’s a panic during initialization, the already
initialized elements would leak but it wouldn’t cause UB.
use std::mem::{self, MaybeUninit};
use std::ptr;
use typed_arena::Arena;
unsafe fn transmute_uninit<A>(r: &mut [MaybeUninit<A>]) -> &mut [A] {
mem::transmute(r)
}
const COUNT: usize = 2;
let arena: Arena<String> = Arena::new();
arena.reserve_extend(COUNT);
let slice: &mut [String];
unsafe {
// Perform initialization before we claim the memory.
let uninitialized = arena.uninitialized_array();
assert!((*uninitialized).len() >= COUNT); // Ensured by the reserve_extend
for elem in &mut (*uninitialized)[..COUNT] {
ptr::write(elem.as_mut_ptr(), "Hello".to_owned());
}
let addr = (*uninitialized).as_ptr() as usize;
// The alloc_uninitialized returns the same memory, but "confirms" its allocation.
slice = transmute_uninit(arena.alloc_uninitialized(COUNT));
assert_eq!(addr, slice.as_ptr() as usize);
assert_eq!(slice, &["Hello".to_owned(), "Hello".to_owned()]);
}sourcepub fn reserve_extend(&self, num: usize)
pub fn reserve_extend(&self, num: usize)
Makes sure there’s enough continuous space for at least num elements.
This may save some work if called before alloc_extend. It also
allows somewhat safer use pattern of alloc_uninitialized.
On the other hand this might waste up to n - 1 elements of space. In case new allocation
is needed, the unused ones in current chunk are never used.
sourcepub fn uninitialized_array(&self) -> *mut [MaybeUninit<T>]
pub fn uninitialized_array(&self) -> *mut [MaybeUninit<T>]
Returns unused space.
This unused space is still not considered “allocated”. Therefore, it
won’t be dropped unless there are further calls to alloc,
alloc_uninitialized, or
alloc_extend which is why the method is safe.
It returns a raw pointer to avoid creating multiple mutable references to the same place.
It is up to the caller not to dereference it after any of the alloc_ methods are called.
sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Convert this Arena into a Vec<T>.
Items in the resulting Vec<T> appear in the order that they were
allocated in.
§Example
use typed_arena::Arena;
let arena = Arena::new();
arena.alloc("a");
arena.alloc("b");
arena.alloc("c");
let easy_as_123 = arena.into_vec();
assert_eq!(easy_as_123, vec!["a", "b", "c"]);sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns an iterator that allows modifying each value.
Items are yielded in the order that they were allocated.
§Example
use typed_arena::Arena;
#[derive(Debug, PartialEq, Eq)]
struct Point { x: i32, y: i32 };
let mut arena = Arena::new();
arena.alloc(Point { x: 0, y: 0 });
arena.alloc(Point { x: 1, y: 1 });
for point in arena.iter_mut() {
point.x += 10;
}
let points = arena.into_vec();
assert_eq!(points, vec![Point { x: 10, y: 0 }, Point { x: 11, y: 1 }]);
§Immutable Iteration
Note that there is no corresponding iter method. Access to the arena’s contents
requries mutable access to the arena itself.
use typed_arena::Arena;
let mut arena = Arena::new();
let x = arena.alloc(1);
// borrow error!
for i in arena.iter_mut() {
println!("i: {}", i);
}
// borrow error!
*x = 2;source§impl Arena<u8>
impl Arena<u8>
sourcepub fn alloc_str(&self, s: &str) -> &mut str
pub fn alloc_str(&self, s: &str) -> &mut str
Allocates a string slice and returns a mutable reference to it.
This is on Arena<u8>, because string slices use byte slices ([u8]) as their backing
storage.
§Example
use typed_arena::Arena;
let arena: Arena<u8> = Arena::new();
let hello = arena.alloc_str("Hello world");
assert_eq!("Hello world", hello);Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Arena<T>
impl<T> !RefUnwindSafe for Arena<T>
impl<T> Send for Arena<T>where
T: Send,
impl<T> !Sync for Arena<T>
impl<T> Unpin for Arena<T>where
T: Unpin,
impl<T> UnwindSafe for Arena<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self with the
fg()
set to
Color::BrightBlack.
§Example
println!("{}", value.bright_black());source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self with the
fg()
set to
Color::BrightGreen.
§Example
println!("{}", value.bright_green());source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self with the
fg()
set to
Color::BrightYellow.
§Example
println!("{}", value.bright_yellow());source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self with the
fg()
set to
Color::BrightMagenta.
§Example
println!("{}", value.bright_magenta());source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self with the
fg()
set to
Color::BrightWhite.
§Example
println!("{}", value.bright_white());source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightBlack.
§Example
println!("{}", value.on_bright_black());source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightGreen.
§Example
println!("{}", value.on_bright_green());source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightYellow.
§Example
println!("{}", value.on_bright_yellow());source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightBlue.
§Example
println!("{}", value.on_bright_blue());source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightMagenta.
§Example
println!("{}", value.on_bright_magenta());source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightCyan.
§Example
println!("{}", value.on_bright_cyan());source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self with the
bg()
set to
Color::BrightWhite.
§Example
println!("{}", value.on_bright_white());source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self with the
attr()
set to
Attribute::Underline.
§Example
println!("{}", value.underline());source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self with the
attr()
set to
Attribute::RapidBlink.
§Example
println!("{}", value.rapid_blink());source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);