Giter Club home page Giter Club logo

another-option's Introduction

another-option

This package provides Opt<T> as an alternative to Option<T>. Why would you want another option? Opt provides advantages when:

  1. the generic type, T, is expensive to allocate, and
  2. mutation between None and Some(...) is frequent.

Examples

Since Rust's built-in Option<T> is an enum, it will drop its Some(...) value when None is assigned.

let mut option: Option<String> = Some(String::with_capacity(1024));
option = None; // drops the string
option = Some(String::with_capacity(1024)); // allocation

Since Opt<T> always owns the value, even when empty, the value can be reused without drops or allocations:

use crate::another_option::Opt;
let mut opt: Opt<String> = Opt::some(String::with_capacity(1024));
opt.map_in_place(|v| v.push_str("value"));
opt.set_none(); // does *not* drop the string
opt.set_some();
assert_eq!(opt.unwrap(), String::from("value"));

another-option's People

Watchers

 avatar  avatar

another-option's Issues

Some consequences of how equality is defined

Given the current code, some unexpected behavior is possible. Consider this example:

let opt_1 = Opt::none(1);
let opt_2 = Opt::none(2);
assert_eq!(opt_1, opt_2); // First, the two variables are equal.
opt_1.set_some(); // Next, the same function ...
opt_2.set_some(); // ... is applied to both.
assert_ne!(opt_1, opt_2); // Now, the two are no longer equal.

The example shows two values that start off being equal. After having the same function applied (set_some()), they are no longer equal. This seems problematic in some sense, even though the definition of equality seems reasonable:

From the source code:

impl<T> PartialEq for Opt<T>
where
    T: std::cmp::PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        if self.empty && other.empty {
            true
        } else if !self.empty && !other.empty {
            self.value == other.value
        } else {
            false
        }
    }
}

I need to think about this more.

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.