Giter Site home page Giter Site logo

zonbi's Introduction

Zonbi

This is an experiment to make it possible to type-erase non-'static types.

How it works

With #[derive(Zonbi)], the type gets an implementation for getting a version of the type where all lifetimes are replaced with the given one. Manual implementation is unsafe because the user must assure that the Casted type is the same as the one of the implementer.

This is used in ZonbiId, a wrapper around TypeId, which different from its inside value, has the additional definition of behaviour for non-'static types. ZonbiId is unique for every type, excluding its lifetimes. Under the hood, it just uses the Zonbi trait to get the 'static version of the type and gets its TypeId.

To hold such type-erased value inside for example a box, you can create a Cage<'life, Z> of the zonbi Z and then hold that in a dyn AnyZonbi<'life> with the associated minimal lifetime. Every zonbi that lives for at least 'life can be upcasted into this trait and downcasted back into it with all the lifetimes being this mininal 'life one.

Example

use zonbi::*;

#[derive(Zonbi)]
struct MyStruct<'a> {
    val: &'a NonCopyI32,
}

type_map(&NonCopyI32(42));

fn type_map<'a>(a: &'a NonCopyI32) {
    let my_struct = MyStruct { val: a };

    let mut type_map: HashMap<ZonbiId, Box<dyn AnyZonbi<'a>>> = HashMap::new();
    let id = ZonbiId::of::<MyStruct>();

    type_map.insert(id, Box::new(Cage::new(my_struct)));

    let r: &MyStruct<'a> = type_map[&id].downcast_ref::<MyStruct<'a>>().unwrap();
    assert_eq!(r.val, &NonCopyI32(42));
}

This is a broken down snippet of the type_map example.

License

Dual-licensed under Apache-2.0 and MIT

zonbi's People

Contributors

daslixou avatar

Stargazers

 avatar Matteo Bigoi 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.