Skip to main content

State

Struct State 

Source
pub struct State { /* private fields */ }
Expand description

类型安全的键值存储容器。

State 用于按类型存储和检索值,是 StateConfigs 的底层实现。 可被 RequestResponse 用来存储从底层协议派生的额外数据。

Implementations§

Source§

impl State

Source

pub fn new() -> State

Create an empty State.

Source

pub fn insert<T: Send + Sync + Clone + 'static>(&mut self, val: T) -> Option<T>

Insert a type into this State.

If an extension of this type already existed, it will be returned.

§Example
let mut cfg = State::new();
assert!(cfg.insert(5i32).is_none());
assert!(cfg.insert(4u8).is_none());
assert_eq!(cfg.insert(9i32), Some(5i32));
Source

pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>

Get a reference to a type previously inserted on this State.

§Example
let mut cfg = State::new();
assert!(cfg.get::<i32>().is_none());
cfg.insert(5i32);

assert_eq!(cfg.get::<i32>(), Some(&5i32));
Source

pub fn remove<T: Send + Sync + Clone + 'static>(&mut self) -> Option<T>

Remove a type from this State.

If aa extension of this type existed, it will be returned.

§Example
let mut cfg = State::new();
cfg.insert(5i32);
assert_eq!(cfg.remove::<i32>(), Some(5i32));
assert!(cfg.get::<i32>().is_none());
Source

pub fn clear(&mut self)

Clear the State of all inserted extensions.

§Example
let mut cfg = State::new();
cfg.insert(5i32);
cfg.clear();

assert!(cfg.get::<i32>().is_none());
Source

pub fn is_empty(&self) -> bool

Check whether the extension set is empty or not.

§Example
let mut cfg = State::new();
assert!(cfg.is_empty());
cfg.insert(5i32);
assert!(!cfg.is_empty());
Source

pub fn len(&self) -> usize

Get the numer of extensions available.

§Example
let mut cfg = State::new();
assert_eq!(cfg.len(), 0);
cfg.insert(5i32);
assert_eq!(cfg.len(), 1);
Source

pub fn extend_from(&mut self, other: &State)

将另一个 State 的内容合并进来(浅拷贝 Arc 值)

Trait Implementations§

Source§

impl Clone for State

Source§

fn clone(&self) -> State

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for State

Source§

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

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

impl Default for State

Source§

fn default() -> State

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

Auto Trait Implementations§

§

impl Freeze for State

§

impl !RefUnwindSafe for State

§

impl Send for State

§

impl Sync for State

§

impl Unpin for State

§

impl UnsafeUnpin for State

§

impl !UnwindSafe for State

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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> 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