Git Product home page Git Product logo

reborrow's Introduction

Emulate reborrowing for user types.

A generalized reference is a type that has reference semantics, without actually being a native Rust reference (like &T and &mut T). e.g.:

struct MyRefMut<'borrow> {
    a: &'borrow mut i32,
    b: &'borrow mut i32,
}

Given a &'a [mut] reference of a &'b view over some owned object, reborrowing it means getting an active &'a view over the owned object, which renders the original reference inactive until it's dropped, at which point the original reference becomes active again.

This crate defines traits to make generalized references more ergonomic to use by giving the ability to borrow and reborrow them.

Features

derive: This imports a derive macro helper for implementing reborrow for user types. It can be used with a Ref/RefMut pair of structs/tuple structs with the same member names, one containing shared references and the other mutable references. The shared variant must be Copy, and the macro is used on the mutable variant and generates the relevant traits for both types.

Examples

This fails to compile since we can't use a non-Copy value after it's moved.

fn takes_mut_option(o: Option<&mut i32>) {}

let mut x = 0;
let o = Some(&mut x);
takes_mut_option(o); // `o` is moved here,
takes_mut_option(o); // so it can't be used here.

This can be worked around by unwrapping the option, reborrowing it, and then wrapping it again.

fn takes_mut_option(o: Option<&mut i32>) {}

let mut x = 0;
let mut o = Some(&mut x);
takes_mut_option(o.as_mut().map(|r| &mut **r)); // "Reborrowing" the `Option`
takes_mut_option(o.as_mut().map(|r| &mut **r)); // allows us to use it later on.
drop(o); // can still be used here

Using this crate, this can be shortened to

use reborrow::ReborrowMut;

fn takes_mut_option(o: Option<&mut i32>) {}

let mut x = 0;
let mut o = Some(&mut x);
takes_mut_option(o.rb_mut()); // "Reborrowing" the `Option`
takes_mut_option(o.rb_mut()); // allows us to use it later on.
drop(o); // can still be used here

The derive macro can be used with structs or tuple structs, and generates the trait definitions for Reborrow and ReborrowMut.

use reborrow::{ReborrowCopyTraits, ReborrowTraits};

#[derive(ReborrowCopyTraits)]
pub struct I32Ref<'a, 'b> {
    pub i: i32,
    pub j: &'a i32,
    pub k: &'b i32,
}

#[derive(ReborrowCopyTraits)]
pub struct I32TupleRef<'a, 'b>(pub i32, pub &'a i32, pub &'b i32);

#[derive(ReborrowTraits)]
#[Const(I32Ref)]
struct I32RefMut<'a, 'b> {
    i: i32,
    #[reborrow]
    j: &'a mut i32,
    #[reborrow]
    k: &'b mut i32,
}

#[derive(ReborrowTraits)]
#[Const(I32TupleRef)]
pub struct I32TupleRefMut<'a, 'b>(
    i32,
    #[reborrow] &'a mut i32,
    #[reborrow] &'b mut i32,
);

reborrow's People

Contributors

sarah-ek 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.