Skip to main content

StoredValue

Struct StoredValue 

Source
pub struct StoredValue<T> { /* private fields */ }
Expand description

存值:不需要响应的数据(Rc 包着的服务、配置、缓存)放在运行时里,换一个 Copy 的句柄。

和 Signal 一样归创建时的作用域、句柄能随意被闭包捕获,所以不用为每个闭包先克隆一份; 不同的是读不订阅、写不通知,值变了界面也不会更新(要更新就用信号)。作用域释放后再读会 panic, 确实可能已经释放时用 StoredValue::try_with。

Implementations§

Source§

impl<T> StoredValue<T>
where T: 'static,

Source

pub fn new(value: T) -> StoredValue<T>

创建存值,归当前作用域所有

Source

pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R

借用读取(不订阅)。闭包里可以 StoredValue::set:手里的还是读开始时的值

Source

pub fn get(&self) -> T
where T: Clone,

读取一份(不订阅)

Source

pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>

和 StoredValue::with 一样借用读取,已经释放时是 None

Source

pub fn set(&self, value: T)

换成新的值(不通知谁)。已经释放时什么都不做

Source

pub fn update(&self, f: impl FnOnce(&mut T))

原地修改(不通知谁)。已经释放时什么都不做;正在读它(在它自己的 StoredValue::with 闭包里)时 panic

Source

pub fn dispose(self)

释放。用于随数据创建的存值,删除数据时调用;平时交给作用域

Trait Implementations§

Source§

impl<T> Clone for StoredValue<T>

Source§

fn clone(&self) -> StoredValue<T>

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<T> Copy for StoredValue<T>

Source§

impl<T> Debug for StoredValue<T>

Source§

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

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

impl<T> Eq for StoredValue<T>

Source§

impl<T> PartialEq for StoredValue<T>

Source§

fn eq(&self, other: &StoredValue<T>) -> bool

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

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T> !Send for StoredValue<T>

§

impl<T> !Sync for StoredValue<T>

§

impl<T> Forget for StoredValue<T>

§

impl<T> Freeze for StoredValue<T>
where PhantomData<(fn() -> T, *const ())>: Freeze,

§

impl<T> RefUnwindSafe for StoredValue<T>

§

impl<T> Unpin for StoredValue<T>
where PhantomData<(fn() -> T, *const ())>: Unpin,

§

impl<T> UnsafeUnpin for StoredValue<T>
where PhantomData<(fn() -> T, *const ())>: UnsafeUnpin,

§

impl<T> UnwindSafe for StoredValue<T>
where PhantomData<(fn() -> T, *const ())>: UnwindSafe,

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> 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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more