Code Monkey home page Code Monkey logo

lifelink's Introduction

lifelink

Erase covariant lifetime parameters from anything, with generic associated types.

[dependencies]
lifelink = { version = "0.1.1" }

Like cryo, lifelink allows you to use the resulting types in dynamic environments where lifetime is unpredictable, like runtimes of garbage-collected scripting languages, or where Any is required. Unlike cryo, the interface is not restricted to primitive references: it works on everything with covariant lifetime parameters though GATs.

The generic_associated_types feature has been stabilized in Rust 1.65. If a pinned nightly compiler before the stabilization release is required, the nightly feature can be enabled which adds the appropriate feature attribute.

Examples

Simple case with just a reference:

use std::thread::spawn;
use std::sync::atomic::{AtomicUsize, Ordering};
use lifelink::{lifelink, Lifelink, RefCtor};

let answer = AtomicUsize::new(0);

lifelink!(lifelink: RefCtor<AtomicUsize> = &answer);

{
    let guard = lifelink.get().unwrap();
    assert_eq!(0, guard.load(Ordering::Relaxed));
    guard.store(42, Ordering::Release);
}

assert_eq!(42, answer.load(Ordering::Acquire));

A more involved example with multiple lifetime parameters, unrelated type parameters. and threads:

use std::thread::spawn;
use std::time::Duration;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::channel;
use std::marker::PhantomData;
use lifelink::{lifelink, Lifelink, Ctor, Cov};

#[derive(Copy, Clone)]
struct Answers<'a, 'b, 'c, T> {
    first: &'a AtomicUsize,
    second: &'b AtomicUsize,
    third: &'c AtomicUsize,
    rest: T,
}

struct AnswersCtor<T> {
    _marker: PhantomData<T>,
}

impl<T: 'static> Ctor for AnswersCtor<T> {
    // The lifetimes can be unified here, due to covariance
    type Ty<'a> = Answers<'a, 'a, 'a, T>;
}

// An invocation of `lifelink::cov!` on the constructor type is required
// to prove covariance to the type system. This only compiles for types that
// can be proved by Rust to be covariant. See docs on the Cov trait for more
// details.
lifelink::cov!(<T: 'static> AnswersCtor<T>);

fn compute<'a, 'b, 'c>(answers: Answers<'a, 'b, 'c, ()>) {
    lifelink!(lifelink: AnswersCtor<()> = answers);
    let (send, recv) = channel();
    
    spawn(move || {
        let guard = lifelink.get().unwrap();
        guard.first.store(42, Ordering::Release);
        guard.second.store(42, Ordering::Release);
        guard.third.store(42, Ordering::Release);
        send.send(()).unwrap();
    });

    // Unlike `cryo`, `lifelink` does *not* attempt to wait until the `'static`
    // handle is dropped. As such, a way to wait for task completion external
    // to `lifelink` is required. See the Caveats section of README for more
    // details, and the rationale behind this decision.
    recv.recv_timeout(Duration::from_millis(20)).unwrap();

    assert_eq!(42, answers.first.load(Ordering::Acquire));
    assert_eq!(42, answers.second.load(Ordering::Acquire));
    assert_eq!(42, answers.third.load(Ordering::Acquire));
}

let first = AtomicUsize::new(0);
let second = AtomicUsize::new(0);
let third = AtomicUsize::new(0);

compute(Answers {
    first: &first,
    second: &second,
    third: &third,
    rest: (),
});

Caveats

Lifelink can only ever give out shared / immutable references. This is because Rust allows moves by default, making mutable references to types with lifetime parameters too hard to reason about, and almost impossible to use correctly unless reduced to uselessness. Instead, users have to use interior mutability in a way that maintains covariance (which, thankfully, Rust will help prove in a Cov impl).

Unlike cryo, lifelink does not attempt to wait until the 'static handle is dropped. It's more than happy to drop or unwrap a Deathtouch, if there isn't a Guard in scope somewhere that precise moment. This may come as surprising, but is a conscious decision to make lifelink work in tandem with environments where lifetime is unpredictable, e.g. a garbage collected scripting language, where it's much better to get a error than a deadlock from a misbehaving script. As such, a way to wait for task completion external to lifelink is required.

Feature flags

  • nightly - Adds feature(generic_associated_types) to the top of the crate, which would allow the crate to compile on a nightly compiler earlier than 1.65 (when the feature was stabilized).

License

MIT OR Apache-2.0

lifelink's People

Contributors

chitoyuu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

lifelink's Issues

Forget unsoundness

When Deathtouch is forgotten (e.g. via mem::forget), this ends the borrow of the original variable without running the drop implementation of Deathtouch. Thus, the lifelink remains usable and this can lead to UB such as aliasing with &mut or use after free:

fn main() {
    use lifelink::{Lifelink, RefCtor};

    let mut value = Box::new(4u8);

    let (mut lifelink, deathtouch) = Lifelink::<RefCtor<u8>>::new(&*value);
    let guard = lifelink.get().unwrap();
    core::mem::forget(deathtouch);

    println!("{}", *guard);
    *value = 8; // guard aliases with mutable access
    println!("{}", *guard);
    drop(value);
    println!("{}", *guard); // use after free
}

Since forgetting is safe, the current API is unsound.

Potential Solutions

  • Have LifeLink::new accept a closure that is provided the lifelink. Then use the scopeguard crate (or the same techniques as scopeguard) to ensure that the code to reclaim the borrow from the lifelink always runs when that closure completes or panics.
  • Use a macro instead of LifeLink::new, that macro will place the Deathtouch in a local that is automatically dropped at the end of the current scope, with the local defined inside the macro, user code will not be able to forget it since it can't be named outside of the macro (e.g. similar to scopeguard::defer!).

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.