Coder Social home page Coder Social logo

iq-scm / staticvec Goto Github PK

View Code? Open in Web Editor NEW

This project forked from slightlyoutofphase/staticvec

0.0 0.0 0.0 1.38 MB

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

Home Page: https://crates.io/crates/staticvec

License: Apache License 2.0

Shell 0.42% Rust 99.55% Batchfile 0.03%

staticvec's Introduction

Latest Version Rustc Version nightly

Build status docs.rs

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" or "not dynamic".

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 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>.

As of version 0.8.5, a fixed-capacity StaticHeap struct based on the standard library BinaryHeap and built around an instance of StaticVec<T, N> has been added as well.

Contributions/suggestions/etc. very welcome!

Minimum supported Rust version: this is a nightly-only crate at the moment due to the use of various feature gates that provide functionality beyond the scope of stabilized minimal subsets such as min_const_generics and min_const_fn.

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);
  }
  // The type parameter is inferred as `StaticVec<usize, 16>`.
  let filled = StaticVec::<_, 6>::filled_with_by_index(|i| {
    staticvec![
      i + 1,
      i + 2,
      i + 3,
      i + 4,
    ]
    .concat(&staticvec![6, 6, 7, 7])
    .intersperse((i + 4) * 4)
  });
  println!("{:?}", filled);
}

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.

staticvec's People

Contributors

5225225 avatar batisteo avatar c410-f3r avatar delamonpansie avatar gavento avatar jswrenn avatar lucretiel avatar nilsirl avatar paulocsanz avatar rsschermer avatar slightlyoutofphase avatar

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.