# Library-level by-move reference

**URL:** <https://internals.rust-lang.org/t/library-level-by-move-reference/8133>\
**Category:** Uncategorized\
**Created:** [August 5, 2018, 8:32am UTC](https://internals.rust-lang.org/t/library-level-by-move-reference/8133 "2018-08-05T08:32:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qnighy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qnighy/32/2195_2.png) [@qnighy](https://internals.rust-lang.org/u/qnighy)\
**Post date:** [August 5, 2018, 8:32am UTC](https://internals.rust-lang.org/t/library-level-by-move-reference/8133/1 "2018-08-05T08:32:19Z")

</div>

As a side project to [unsized rvalues](https://github.com/rust-lang/rust/pull/51131), I published an experimental crate `refmove` that provides by-move reference types.

[https://crates.io/crates/refmove](https://crates.io/crates/refmove)

This is the realization of [my Pre-RFC posted some time ago](https://internals.rust-lang.org/t/pre-rfc-non-builtin-owned-references/6447), but has one new design idea: anchoring for by-move borrowing.

In this crate, you will need a _two-step method call_ to borrow by-move:

```rust
42.anchor().borrow_move()

```

The first method takes `42` by value and produces something similar to `Some(42)`. The second method takes `Some(42)` by mutable reference, takes the ownership and returns a by-move reference. I think it’s necessary because we need to insert a flag and drop hook at the caller side to ensure the validity of the memory region.

Anyway, it basically overlaps with unsized rvalues in its purpose but has both upsides and downsides. I’d like to share this prototype to compare these approaches.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 5, 2018, 8:12pm UTC](https://internals.rust-lang.org/t/library-level-by-move-reference/8133/2 "2018-08-05T20:12:10Z")

</div>

Correct me if I’m wrong… this is like a glorified `std::unique_ptr` from C++, insofar as the style of move semantics (nullify whatever is moved from, instead of making it inaccessible).

---

<div class="post-metadata">

**Author:** ![qnighy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qnighy/32/2195_2.png) [@qnighy](https://internals.rust-lang.org/u/qnighy)\
**Post date:** [August 6, 2018, 1:27pm UTC](https://internals.rust-lang.org/t/library-level-by-move-reference/8133/3 "2018-08-06T13:27:07Z")

</div>

That’s true. It imposes an additional “only once something” contract other than that of types. The problem is, if we want to ensure “only once borrowing”, then we need `&move T`, which is exactly what I want to emulate here.

A similar contract is used in `Future::poll`: it can be called only once, except for calls that returned `Pending`.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/library-level-by-move-reference/8133/4 "2019-03-25T08:30:39Z")

</div>

Closed on March 25, 2019, 8:30am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
