Coder Social home page Coder Social logo

tupperware's Introduction

Tupperware

crate version docs

Crate that lets you decide if you want to put your types into a Box. It allows you to express polymorphism over how your data is stored.

Explanation

Sometimes you have a struct, in which you would like to store the data differently depending on circumstance. Some examples are:

  • You want to be able to switch between using a Rc and an Arc, depending on the presence of a feature that enables support for multithreading (while paying the cost of using an atomic reference counter).
  • You want to be able to store references to data in circumstances.

Tupperware can help here, by allowing you to use the type system to switch between different data storage variations. You can write your struct definitions semantially, capturing what data each field should hold, and then switch between different storage representations of your data.

Usage

For example, you can define a struct such as this:

use tupperware::{Storage, Inline};

type OrderId = i64;

#[derive(Debug, Clone)]
struct MyData<S: Storage = Inline> {
    name: S::Value<str>,
    orders: S::Value<[OrderId]>,
}

Now, depending on the Storage type parameter, your values will be stored differently. Here is how the struct would be stored for each of the default storage strategies:

Inline
#[derive(Debug, Clone)]
struct MyData {
    name: String,
    orders: Vec<OrderId>,
}
Box
#[derive(Debug, Clone)]
struct MyData {
    name: Box<str>,
    orders: Box<[OrderId]>,
}
Arc
#[derive(Debug, Clone)]
struct MyData {
    name: Arc<str>,
    orders: Arc<[OrderId]>,
}
Rc
#[derive(Debug, Clone)]
struct MyData {
    name: Rc<str>,
    orders: Rc<[OrderId]>,
}
Ref<'a>
#[derive(Debug, Clone)]
struct MyData {
    name: &'a str,
    orders: &'a [OrderId]>,
}

This example shows how the storage strategy now lets you control how your type is stored. It allows you to specialize the storage mechanism easily, for example swapping out Rc for Arc if a given feature is enabled:

#[cfg(feature = "sync")]
pub type MyData = types::MyData<Arc>;
#[cfg(not(feature = "sync"))]
pub type MyData = types::MyData<Rc>;

With this code, you can now use the type parameter to control how your types are stored. This is set to default to storing the type inline, however you have a couple of built-in strategies. Here is an example of how str maps using different strategies:

You can also define your own storage mechanisms by implementing the Storage trait.

License

MIT, see [LICENSE.md][].

tupperware's People

Contributors

xfbs avatar

Stargazers

Benjamin avatar Andreas Motl avatar Mara Schulke avatar

Watchers

Andreas Motl avatar  avatar  avatar

tupperware's Issues

Add derive macro

Add derive macro to automatically derive:

  • Into traits
  • PartialEq traits
  • PartialOrd traits
  • AsRef traits

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.