项目作者: timvermeulen

项目描述 :
A fixed-capacity memory buffer allocated on the stack using const generics.
高级语言: Rust
项目地址: git://github.com/timvermeulen/const-buffer.git
创建时间: 2020-02-03T23:09:49Z
项目社区:https://github.com/timvermeulen/const-buffer

开源协议:Other

下载


const-buffer

crates.io
docs.rs
rustc version

A fixed-capacity memory buffer allocated on the stack using const generics.

This is a low-level utility, useful for implementing higher-level data structures such as fixed-capacity vectors and ring buffers. Since ConstBuffer‘s main purpose is to build safe abstractions on top of, almost its entire API surface is unsafe.

ConstBuffer does not keep track of which elements are in an initialized state. Furthermore, in order to ensure optimal performance, no bounds checks are performed unless debug assertions are enabled. Any misuse of this crate leads to undefined behavior.

Example usage

  1. use const_buffer::ConstBuffer;
  2. fn main() {
  3. let mut buffer = ConstBuffer::<u32, 10>::new();
  4. unsafe {
  5. buffer.copy_from_slice(2, &[10, 20, 30]);
  6. // [_, _, 10, 20, 30, _, _, _, _, _]
  7. buffer.write(5, 40);
  8. // [_, _, 10, 20, 30, 40, _, _, _, _]
  9. assert_eq!(buffer.get(3), &20);
  10. assert_eq!(buffer.get(2..6), &[10, 20, 30, 40]);
  11. buffer.copy_within(2..6, 0);
  12. // [10, 20, 30, 40, 30, 40, _, _, _, _]
  13. buffer.get_mut(1..4).reverse();
  14. // [10, 40, 30, 20, 30, 40, _, _, _, _]
  15. assert_eq!(buffer.get(..6), &[10, 40, 30, 20, 30, 40]);
  16. buffer.swap(3, 8);
  17. // [10, 40, 30, _, 30, 40, _, _, 20, _]
  18. assert_eq!(buffer.read(8), 20);
  19. }
  20. }

License

Licensed under either of Apache License, Version 2.0 or MIT License at your option. Any source contributions will be dual-licensed in the same way.