GithubHelp home page GithubHelp logo

braunse / eso Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 88 KB

Type-level building blocks for making Cow-like containers in Rust

Home Page: https://docs.rs/eso

License: Mozilla Public License 2.0

Rust 100.00%
rust lifetimes newtypes references

eso's Introduction

Eso - type-level building block for making Cow-like containers

Crates.io docs.rs GitHub issues GitHub pull requests GitHub last commit GitHub Workflow Status Crates.io

This library provids the Eso struct, a versatile building block for making newtypes that may own or reference their contents.

How to use

Add to your Cargo.toml like:

[dependencies]
eso = "0.0.0"

Example

Here is how to make a basic Cow-like type:

use eso::t;

pub struct SmartString<'a>(t::SO<&'a str, &'a str, String>);

impl SmartString {
    fn from_ref(c: &'static str) -> Self {
        SmartString(t::SO::from_static(c))
    }

    fn from_string(s: String) -> Self {
        SmartString(t::SO::from_owned(s))
    }

    fn into_owned(self) -> String {
        self.0.into_owning().safe_unwrap_owned()
    }

    fn is_owned(&self) -> bool {
        self.0.is_owning()
    }

    fn is_borrowed(&self) -> bool {
        self.0.is_reference()
    }

    fn to_mut(&mut self) -> &mut String {
        self.0.to_mut()
    }
}

impl Deref for SmartString {
    type Target = str;

    fn deref(&self) -> &str {
        self.0.get_ref()
    }
}

Details

Eso is very flexible, because it is meant as a building block for library authors who will restrict its flexibility to make sense for their respective use cases:

  • The Eso type itself can represent a choice out of any subset of

    • a borrowed reference
    • a static or shared reference
    • an owned value

    Which of these variants exist in the Eso type depends on the type parameters and can vary between usages in the client code.

  • Eso generalizes references and ownership.

    For example, you can make a Cow-like type that stores a custom type instead of a normal reference, so you could make a copy-on-write OwningRef

The price for this flexibility is ergonomics. When using eso the types can get rather long and the where-clauses in the library are rather unwieldy.

To Do

  • More API docs
  • More tests
  • More examples

eso's People

Contributors

braunse avatar

Watchers

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