Struct algorithm::Slab

source ·
pub struct Slab<T: Default> { /* private fields */ }
Expand description

一个缓存对象的实现, 类似linux中的slab 将一个对象重复循环使用, 避免频繁分配数据的可能 得出的对象可能未重新初始化, 为上一次的最终值, 请按需重新初始化 默认的初始化依赖Default接口, 请实现Default

§Examples

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    for _ in 0..100 {
        let k = slab.get_next();
        slab[&k] = format!("{}", k);
    }
    assert!(slab.len() == 100);
    for i in 0..100 {
        let _ = slab.remove(i);
    }
    assert!(slab.len() == 0);
    let k = slab.get_next();
    assert!(k == 99);
    assert!(slab[&k] == "99");
    let k = slab.get_reinit_next();
    assert!(k == 98);
    assert!(slab[&k] == "");
}

Implementations§

source§

impl<T: Default> Slab<T>

source

pub fn new() -> Self

source

pub fn len(&self) -> usize

获取当前长度

source

pub fn is_empty(&self) -> bool

是否为空表

source

pub fn clear(&mut self)

清除数据

source

pub fn get(&mut self, key: usize) -> &T

获取index值相对应的value值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("slab");
    assert!(slab.get(k) == &"slab");
}
source

pub fn try_get(&mut self, key: usize) -> Option<&T>

尝试获取key下的值

source

pub fn get_mut(&mut self, key: usize) -> &mut T

获取index值相对应的value可变值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("slab");
    assert!(slab.get_mut(k) == &mut "slab");
}
source

pub fn get_next_val(&mut self) -> (usize, &mut T)

获取下一个的key值和val值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("slab");
    assert!(slab.get_next_val() == (1, &mut ""));
}
source

pub fn get_next(&mut self) -> usize

获取下一个的key值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("slab");
    assert!(slab.get_next() == 1);
}
source

pub fn insert(&mut self, val: T) -> usize

插入一条数据进入slab缓存起来

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("slab");
    assert!(slab[&k] == "slab");
}
source

pub fn remove(&mut self, key: usize)

删除某个键值数据, 不会返回内容, 因为该内容会提供给下次复用

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.get_next();
    let k1 = slab.get_next();
    slab[&k1] = "slab";
    assert!(slab.len() == 2);
    slab.remove(k);
    assert!(slab.len() == 1);
    assert!(slab[&k1] == "slab");
}
source

pub fn try_remove(&mut self, key: usize) -> bool

试图删除某个键值数据, 不会返回内容, 因为该内容会提供给下次复用

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.get_next();
    let k1 = slab.get_next();
    slab[&k1] = "slab";
    assert!(slab.len() == 2);
    assert!(slab.try_remove(k) == true);
    assert!(slab.try_remove(k) == false);
    assert!(slab.len() == 1);
    assert!(slab[&k1] == "slab");
}
source

pub fn contains_key(&mut self, k: usize) -> bool

是否包含某个键值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.get_next();
    slab[&k] = "slab";
    assert!(slab.contains_key(k) == true);
    assert!(slab.try_remove(k) == true);
    assert!(slab.contains_key(k) == false);
}
source

pub fn iter(&self) -> Iter<'_, T>

遍历当前的所有值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    slab.insert("hello");
    slab.insert("this");
    let mut iter = slab.iter();
    assert!(iter.next() == Some((0, &"hello")));
    assert!(iter.next() == Some((1, &"this")));
    assert!(iter.next() == None);
}
source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

遍历当前的所有值, 可同时修改值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    slab.insert("slab".to_string());
    slab.insert("this".to_string());
    for (k, v) in slab.iter_mut() {
        v.push_str(" ok")
    }
    assert!(slab[&0] == "slab ok");
}
source

pub fn drain(&mut self) -> Drain<'_, T>

排除当时所有的值

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    slab.insert("slab".to_string());
    slab.insert("this".to_string());
    {
        let mut drain = slab.drain();
        assert!(drain.next()==Some("slab".to_string()));
    }
    assert!(slab.len() == 0);
}
source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(usize, &mut T) -> bool,

根据保留当前的元素, 返回false则表示抛弃元素

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    slab.insert("hello");
    slab.insert("this");
    slab.insert("year");
    slab.retain(|_, v| *v == "hello" || *v == "this");
    assert!(slab.len() == 2);
    assert!(slab.get(1) == &"this");
}
source§

impl<T: Default + Reinit> Slab<T>

source

pub fn get_reinit_next(&mut self) -> usize

获取下一个key并重新初始化

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("hello");
    let k1 = slab.insert("slab");
    slab.remove(k1);
    assert!(slab.get_reinit_next() == k1);
    assert!(slab.get(k1) == &"");
}
source

pub fn get_reinit_next_val(&mut self) -> (usize, &mut T)

获取下一个key和val并重新初始化

use algorithm::Slab;
fn main() {
    let mut slab = Slab::new();
    let k = slab.insert("hello");
    let k1 = slab.insert("slab");
    slab.remove(k1);
    assert!(slab.get_reinit_next_val() == (k1, &mut ""));
}

Trait Implementations§

source§

impl<T: Default + Clone> Clone for Slab<T>

source§

fn clone(&self) -> Self

Returns a copy 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<T: Debug + Default> Debug for Slab<T>

source§

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

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

impl<T: Default> Extend<T> for Slab<T>

source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T: Default> FromIterator<T> for Slab<T>

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Slab<T>

Creates a value from an iterator. Read more
source§

impl<'a, T: Default> Index<&'a usize> for Slab<T>

source§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: &usize) -> &T

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, T: Default> IndexMut<&'a usize> for Slab<T>

source§

fn index_mut(&mut self, index: &usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<T: Default> IntoIterator for Slab<T>

source§

type Item = (usize, T)

The type of the elements being iterated over.
source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
source§

impl<T: Send + Default> Send for Slab<T>

source§

impl<T: Sync + Default> Sync for Slab<T>

Auto Trait Implementations§

§

impl<T> Freeze for Slab<T>

§

impl<T> RefUnwindSafe for Slab<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Slab<T>
where T: Unpin,

§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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 = 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.