Skip to main content

PolicyState

Struct PolicyState 

Source
pub struct PolicyState {
    pub velocity_stamps: Vec<u64>,
    pub category_spent_cents: BTreeMap<String, u64>,
}
Expand description

每委托的策略运行时状态:随 commit 演化、随回放重建。

只在对应策略维度启用时才记录对应数据(未启用速率不记时刻,未设上限的类目 不记账),因此缺省策略下本结构恒空、零额外内存。时刻全量保留、不做剪枝: 与账本同阶(每笔成功放行一个 u64,审计本身也在按行增长),剪枝会破坏 「访问器 = 全量成功时刻」的直读语义。

Fields§

§velocity_stamps: Vec<u64>

成功放行时刻(仅启用速率时记录;按时间升序)。

§category_spent_cents: BTreeMap<String, u64>

类目累计消费(仅设了上限的类目记账)。

Implementations§

Source§

impl PolicyState

Source

pub fn in_window_count(&self, now: u64, window_secs: u64) -> usize

now 时刻窗口内的成功放行笔数。

一笔在 t 时刻放行,当 now - t < window_secs 时计入;恰在 t + window_secs 时刻不再计入(半开,与过期语义同形)。

Source

pub fn record_velocity_stamp(&mut self, now: u64)

记录一笔成功放行的时刻(仅当该委托启用了速率限制才调用——未启用 速率的委托不产生任何窗口时刻,velocity_stamps 恒空)。

Source

pub fn record_category_spend(&mut self, category: &str, amount_cents: u64)

记一笔类目消费(仅当该类目设了上限才调用——未设上限的类目没有 「类目预算」可言,不记账)。

§前提

amount_cents 的溢出必须由调用方先经 checked_add 判过(闸在阶段 5 里先验溢出再 commit);本方法不重复判,溢出 panic 会在回放与实时两侧 同位触发,不破坏「实时态 == 回放态」。

Trait Implementations§

Source§

impl Clone for PolicyState

Source§

fn clone(&self) -> PolicyState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PolicyState

Source§

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

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

impl Default for PolicyState

Source§

fn default() -> PolicyState

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

impl<'de> Deserialize<'de> for PolicyState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for PolicyState

Source§

impl PartialEq for PolicyState

Source§

fn eq(&self, other: &PolicyState) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for PolicyState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for PolicyState

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.