pub struct State { /* private fields */ }Expand description
类型安全的键值存储容器。
State 用于按类型存储和检索值,是 State 和 Configs 的底层实现。
可被 Request 和 Response 用来存储从底层协议派生的额外数据。
Implementations§
Source§impl State
impl State
Sourcepub fn insert<T: Send + Sync + Clone + 'static>(&mut self, val: T) -> Option<T>
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));Sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
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));Sourcepub fn remove<T: Send + Sync + Clone + 'static>(&mut self) -> Option<T>
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());Sourcepub fn clear(&mut self)
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());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn extend_from(&mut self, other: &State)
pub fn extend_from(&mut self, other: &State)
将另一个 State 的内容合并进来(浅拷贝 Arc 值)
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more