Git Product home page Git Product logo

const-buffer's Introduction

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

use const_buffer::ConstBuffer;

fn main() {
    let mut buffer = ConstBuffer::<u32, 10>::new();

    unsafe {
        buffer.copy_from_slice(2, &[10, 20, 30]);
        // [_, _, 10, 20, 30, _, _, _, _, _]
        buffer.write(5, 40);
        // [_, _, 10, 20, 30, 40, _, _, _, _]
        assert_eq!(buffer.get(3), &20);
        assert_eq!(buffer.get(2..6), &[10, 20, 30, 40]);

        buffer.copy_within(2..6, 0);
        // [10, 20, 30, 40, 30, 40, _, _, _, _]
        buffer.get_mut(1..4).reverse();
        // [10, 40, 30, 20, 30, 40, _, _, _, _]
        assert_eq!(buffer.get(..6), &[10, 40, 30, 20, 30, 40]);

        buffer.swap(3, 8);
        // [10, 40, 30, _, 30, 40, _, _, 20, _]
        assert_eq!(buffer.read(8), 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.

const-buffer's People

Contributors

timvermeulen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

const-buffer's Issues

Consider switching to `MaybeUninit<[T; N]>` rather than `[MaybeUninit<T>; N]` before you get too far along with this.

Just based on my experience writing staticvec, [MaybeUninit<T>; N] has no real tangible advantages over MaybeUninit<[T; N]> for a crate that is already (necessarily) making heavy use of unsafe code internally and is specifically based around const generics.

I started out using [MaybeUninit<T>; N] as the internal array for StaticVec, but switched to MaybeUninit<[T; N]> after about a month or so as it's just significantly more ergonomic to work with in every way.

For example, if using MaybeUninit<[T; N]>, your current from_maybe_uninit_array could be rewritten to simply take a normal array with the only code being MaybeUninit::new(array).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.