StaticVec

实现了一个固定容量的堆栈分配的Vec替代方案,由一个数组支持,使用常数泛型。「Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.」

Github星跟蹤圖

Latest Version Rustc Version nightly

Build Status
Build status

Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.

Note: the word "static" here is meant by the traditional definition of "unchanging" / "not dynamic" etc.

This crate does not use literal static variables for anything (but does provide multiple ways
to instantiate a StaticVec as a static or const variable if desired).

Fully #![no_std] compatible (with almost no loss of functionality) by setting
default-features = false for the staticvec dependency in your Cargo.toml.

Optional support for serialization and deserialization of the StaticVec struct
via serde is available by activating the serde_support crate feature.

StaticVec also implements both Deref and DerefMut to [T], meaning that all existing slice
methods are accessible through instances of it and that references to it can be used in contexts
where [T] is expected.

As of version 0.8.0, this crate additionally provides a fixed-capacity StaticString struct, which is built
around an instance of StaticVec<u8, N>.

Contributions/suggestions/etc. very welcome!

Minimum supported Rust version: due to the use of const generics, this is a nightly-only crate at the moment.

A basic usage example:

use staticvec::{staticvec, StaticVec};

fn main() {
  let mut v = StaticVec::<usize, 64>::new();
  for i in 0..v.capacity() {
    v.push(i);
  }
  for i in &v {
    println!("{}", i);
  }
  v.clear();
  v.insert(0, 47);
  v.insert(1, 48);
  v.insert(2, 49);
  v.insert(v.len() - 1, 50);
  v.insert(v.len() - 2, 51);
  v.insert(v.len() - 3, 52);
  for i in &v {
    println!("{}", i);
  }
  for i in &v.reversed().drain(2..4) {
    println!("{}", i);
  }
  while v.is_not_empty() {
    println!("{}", v.remove(0));
  }
  for f in staticvec![12.0, 14.0, 15.0, 16.0].iter().skip(2) {
    println!("{}", f);
  }
  for i in staticvec![
    staticvec![14, 12, 10].sorted(),
    staticvec![20, 18, 16].reversed(),
    staticvec![26, 24, 22].sorted(),
    staticvec![32, 30, 28].reversed(),
  ]
  .iter()
  .flatten()
  .collect::<StaticVec<usize, 12>>()
  .iter() {
    println!("{}", i);
  }
}

License:

Licensed under either the MIT license or version 2.0 of the Apache License. Your choice as to which!
Any source code contributions will be dual-licensed in the same fashion.

主要指標

概覽
名稱與所有者slightlyoutofphase/staticvec
主編程語言Rust
編程語言Rust (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2019-08-02 02:37:58
推送於2023-05-29 00:37:17
最后一次提交2023-05-28 20:37:17
發布數34
最新版本名稱0.11.9 (發布於 )
第一版名稱0.7.5 (發布於 )
用户参与
星數268
關注者數5
派生數19
提交數1.3k
已啟用問題?
問題數32
打開的問題數6
拉請求數18
打開的拉請求數0
關閉的拉請求數11
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?