Skip to main content

ScratchArena

Struct ScratchArena 

Source
pub struct ScratchArena<const CAP: usize = SCRATCH_CAPACITY> { /* private fields */ }
Expand description

基于 bumpalo::Bump 的帧级瞬态内存池,帧末 rewind() 零分配复用。

Implementations§

Source§

impl<const CAP: usize> ScratchArena<CAP>

Source

pub fn new() -> Self

创建一个新的瞬态帧内存池。

Source

pub fn try_alloc<'a, T>(&'a self, val: T) -> Option<&'a mut T>

尝试在帧池中无 panic 分配单个对象。

Source

pub fn alloc<'a, T>(&'a self, val: T) -> &'a mut T

在帧池中分配单个节点或事件闭包(返回 'a 可变引用,彻底消除 Box 堆分配)。

Source

pub fn try_alloc_temp<'a, T>(&'a self, val: T) -> Option<&'a mut T>

尝试在帧池中无 panic 分配临时树节点。

Source

pub fn alloc_temp<'a, T>(&'a self, val: T) -> &'a mut T

分配仅在布局求解阶段存活的临时树节点(ElementNode / Element / DivVisual)。

Source

pub fn alloc_bytes<'a>(&'a self, len: usize) -> &'a mut [u8] ⓘ

在帧池中分配长度为 len 的对齐字节切片 &'a mut [u8]。

Source

pub fn alloc_iter<'a, T, I>(&'a self, iter: I) -> &'a mut [T]
where I: IntoIterator<Item = T>,

无需 Vec 中转,直接将任意迭代器原地展开为连续内存切片 &'a mut [T]。

Source

pub fn alloc_str<'a>(&'a self, s: &str) -> StrRef<'a>

将任意临时字符串切片 s 拷贝至当前 ScratchArena,返回 StrRef<'a>。

Source

pub fn alloc_fmt<'a>(&'a self, args: Arguments<'_>) -> StrRef<'a>

将 format_args! 字节流直接写入当前 ScratchArena,返回 StrRef<'a>。

Source

pub fn rewind(&mut self)

重置帧内存池游标,实现零分配跨帧复用。

Source

pub fn save_mark(&self) -> usize

保存当前竞技场游标水位线(用于阶段性临时数据分时复用回收)。

Source

pub fn restore_mark(&mut self, mark: usize)

恢复竞技场游标至指定水位线(需要可变引用 &mut self,静态保证此前分配的借用生命周期已结束)。

Source

pub fn used_bytes(&self) -> usize

返回当前帧已使用的逻辑字节数。

Source

pub fn can_alloc_slice<T>(&self, count: usize) -> bool

检查当前竞技场剩余空间是否足以容纳 count 个连续的 T 元素。

Source

pub fn can_alloc_temp_slice<T>(&self, count: usize) -> bool

检查当前竞技场临时区是否足以分配 count 个 T 元素。

Source

pub fn alloc_temp_iter_exact<'a, T, I>( &'a self, count: usize, iter: I, ) -> &'a mut [T]
where I: IntoIterator<Item = T>,

批量分配由固定长度迭代器产生的连续切片 &'a mut [T](供布局求解器构建 LayoutNode 子树使用)。

Trait Implementations§

Source§

impl<const CAP: usize> Default for ScratchArena<CAP>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const CAP: usize = SCRATCH_CAPACITY> !Freeze for ScratchArena<CAP>

§

impl<const CAP: usize = SCRATCH_CAPACITY> !RefUnwindSafe for ScratchArena<CAP>

§

impl<const CAP: usize = SCRATCH_CAPACITY> !Sync for ScratchArena<CAP>

§

impl<const CAP: usize = SCRATCH_CAPACITY> !UnwindSafe for ScratchArena<CAP>

§

impl<const CAP: usize> Forget for ScratchArena<CAP>

§

impl<const CAP: usize> Move for ScratchArena<CAP>

§

impl<const CAP: usize> Send for ScratchArena<CAP>

§

impl<const CAP: usize> Unpin for ScratchArena<CAP>

§

impl<const CAP: usize> UnsafeUnpin for ScratchArena<CAP>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> FluentBuilder for T

Source§

fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self

当 condition 为 true 时执行闭包修饰自身,否则原样返回。
Source§

fn when_else( self, condition: bool, then: impl FnOnce(Self) -> Self, else_fn: impl FnOnce(Self) -> Self, ) -> Self

根据 condition 在两个修饰分支之间二选一。
Source§

fn when_some<O>( self, option: Option<O>, then: impl FnOnce(Self, O) -> Self, ) -> Self

当 option 为 Some(value) 时执行闭包修饰自身,否则原样返回。
Source§

fn when_none<O>( self, option: &Option<O>, then: impl FnOnce(Self) -> Self, ) -> Self

当 option 为 None 时执行闭包修饰自身,否则原样返回。
Source§

fn apply<U>(self, f: impl FnOnce(Self) -> U) -> U

通用管道映射变换(避免与 Iterator::map / Option::map 重名)。
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WasmNotSend for T
where T: Send,