GithubHelp home page GithubHelp logo

gens's Introduction

::gens

Provides Id, a cheap numerical identifier that has the ability to create new, unique Ids without external state, as well as providing basic generational information.

Is this useful?

Strictly speaking, no. It's trivial to have an atomic counter that will give you nice, simple, sequential ID's in a thread-safe manner, no strings attached. But since when was programming strictly useful?

The one thing this provides over a global ID generator is basic generational information. Each Id keeps track of its parent and its depth, allowing you to compare ID's for depth and trace a lineage back to the root.

Why's it so big?

Each Id is 32 bytes to avoid collisions. If you're certain that you're done creating children from a given Id, you can convert it into its final u128 to half that size via .id().

Note that this is a one-way operation: you cannot turn a u128 back into an Id, since information is lost in the process (e.g. generational information).

The one exception is with feature serde: it enables serde support. Note that this serialization loses no information, it serializes the whole Id and thus will never lead to duplicate ID's.

Give me some code

use gens::Id;

let mut root = Id::root();

// Two `::root()`s are identical! Be warned!
let other = Id::root();
assert_eq!(root, other);

// Two `.next_id()`s are unique:
let mut a = root.next_id();
let mut b = root.next_id();
assert_ne!(a, b);

// And those `Id`s can continue to proliferate:
let c = a.next_id();
let d = b.next_id();
assert_ne!(c, d);

// And, they carry some generational info:
assert_eq!(c.parent(), a.id());
assert!(c > root);
assert!(d > b);

gens's People

Contributors

kyllingene avatar

Stargazers

Markus avatar  avatar

Watchers

 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.