Coder Social home page Coder Social logo

idcontain.rs's Introduction

Build Status

idcontain

A Rust library for containers built around 'generational id-s': tagged id-s which prevent errors caused by id reuse, id mixing (between different containers) and enable 'dangling id' detection (using an id after it was deleted).

Currently the only implemented type is IdSlab which is an unordered collection, sometimes called a Slab allocator. It differs from @carllerche's excellent Slab in its support for the properties described above at a small cost in memory and speed.

The particular implementation used for preventing ID reuse and mixing has some caveats (i.e. pathological cases causing false positives in membership tests) which mean you should not rely on it for memory safety or security. These cases are quite extreme so you're definitely OK to use this for video games etc. See the documentation for more details.

An old but relevant blog post about this technique.

Getting Started

Add the dependency to your Cargo.toml manifest.

[dependencies]
idcontain = "0.6"

Example

extern crate idcontain;

use idcontain::{IdSlab, Id};

fn main() {
  let mut id_vec_int1 = IdSlab::new();
  let mut id_vec_int2 = IdSlab::with_capacity(3);
  let mut id_vec_str = IdSlab::with_capacity(1);

  // Inserting an element returns its `Id`.
  let id1: Id<i32> = id_vec_int1.insert(1);
  assert_eq!(id_vec_int1[id1], 1);                // Panicking lookup.
  assert_eq!(id_vec_int1.get(id1), Some(&1));     // Non-panicking lookup.
  id_vec_int1[id1] = 10;                          // In-place mutation.
  assert_eq!(id_vec_int1.remove(id1), Some(10));  // Removal.

  // Id-s are not reused.
  let id2 = id_vec_int1.insert(20);
  assert!(id2 != id1);

  // Id-s from different `IdSlab`-s do not collide.
  let id3 = id_vec_int2.insert(20);
  assert!(id3 != id2);
  assert!(id3 != id1);

  // Id-s from `IdSlab`-s of different types cannot mixed with compile-time
  // checks.
  let str_id: Id<&'static str> = id_vec_str.insert("hello");
  assert_eq!(id_vec_str[str_id], "hello")

  // Compile-time errors:
  //   str_id == id1
  //   id_vec_int1.get(str_id)
  //   id_vec_str[id1]
}

Iteration and other stuff is also supported, check out the documentation!

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.