#borrow #lifetime #macro


Macro for calling a &mut self method with transient &-borrows of self in the parameters

4 releases (2 breaking)

Uses old Rust 2015

0.3.1 Jan 20, 2017
0.3.0 Aug 7, 2016
0.2.0 Jul 7, 2016
0.1.0 Feb 18, 2016

#949 in Rust patterns

Download history 58/week @ 2022-06-02 14/week @ 2022-06-09 29/week @ 2022-06-16 17/week @ 2022-06-23 24/week @ 2022-06-30 14/week @ 2022-07-07 19/week @ 2022-07-14 23/week @ 2022-07-21 14/week @ 2022-07-28 42/week @ 2022-08-04 34/week @ 2022-08-11 18/week @ 2022-08-18 22/week @ 2022-08-25 21/week @ 2022-09-01 45/week @ 2022-09-08 20/week @ 2022-09-15

109 downloads per month
Used in 3 crates

MIT license


This is a tiny Rust crate that works around a common annoyance with the borrow checker.

Sometimes, you try to call a method on an object that takes &mut self, but you need to transiently borrow the object to compute the parameter(s) of the method. For example:

let mut v = vec![1, 2, 3];
v.reserve(v.capacity()); // double the capacity of the vector

This fails to compile with an error saying that you can't borrow v mutably (for reserve) while also borrowing it immutably (for capacity). But logically, the parameters are computed before the method call starts, so what's the issue? The immutable borrow should be over! Unfortunately, the borrow checker doesn't see it that way, since its regions are all "lexical" -- corresponding to an actual range of characters in the source code. We can fix the error by rewriting the code like this:

let mut v = vec![1, 2, 3];
let v_cap = v.capacity();
v.reserve(v_cap); // double the capacity of the vector

Now the immutable borrow begins and ends before the mutable one starts, and borrowck is happy. But this is tedious. If only a macro could do it for us.

#[macro_use] extern crate unborrow;

let mut v = vec![1, 2, 3];

By the way, this issue could conceivably disappear if "non-lexical lifetimes" are ever added to the Rust language.

As an aside, this macro would be interesting to read if you are curious about details of macro hygiene in Rust. The macro exploits hygiene to achieve a "gensym"-like facility: to precompute all arguments to a function in bindings, we need unique names for each of those bindings that don't collide with any other variables in scope. If you look at the pretty-expanded form of the source, it looks like all the bindings are named "arg". But in fact, they are all different because they are tagged with different hygiene contexts. See the source code for more.

No runtime deps