# FnBorrow traits

**URL:** <https://internals.rust-lang.org/t/fnborrow-traits/8136>\
**Category:** Uncategorized\
**Created:** [August 5, 2018, 6:17pm UTC](https://internals.rust-lang.org/t/fnborrow-traits/8136 "2018-08-05T18:17:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffBurdges](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffburdges/32/2116_2.png) [@JeffBurdges](https://internals.rust-lang.org/u/JeffBurdges)\
**Post date:** [August 5, 2018, 6:17pm UTC](https://internals.rust-lang.org/t/fnborrow-traits/8136/1 "2018-08-05T18:17:39Z")

</div>

I love `Borrow` and `BorrowMut` but algorithms frequently want to borrow a “view” into a data structure. It’d be very cool if closures could be used like mutable borrows, but they cannot because you cannot make `FnOnce::Output` dependent on the lifetime of the `&mut self` parameter of `FnMut::call_mut`.

Would this even be possible? Maybe:

```rust
pub trait FnOnce<Args> {
type Output<'fn>;
    extern "rust-call" fn call_once(self, args: Args) -> Self::Output<'static>;
}
pub trait FnMut<Args>: FnOnce<Args> {
    extern "rust-call" fn call_mut<'fn>(&'fn mut self, args: Args) -> Self::Output<'fn>;
}
pub trait Fn<Args>: FnMut<Args> {
    extern "rust-call" fn call<'fn>(&'fn self, args: Args) -> Self::Output<'fn>;
}

```

Is this doable? I.e. can `FnOnce::call_once` actually call `FnMut::call_mut`? I’d think no because consumed as `self` is not quite the same as `'static`.

Instead, we might consider a second hierarchy, maybe named `View*`, but maybe `FnBorrow*` explains them better:

```rust
pub trait FnBorrowMut<Args<'borrow>> {
    type Output<'borrow>;
    extern "rust-call" fn borrow_mut<'borrow>(&'fn mut self, args: Args<'borrow>) -> Self::Output<'borrow>;
}
pub trait FnBorrow<Args<'borrow>>: FnBorrowMut<Args<'borrow>> {
    extern "rust-call" fn borrow<'borrow>(&'borrow self, args: Args<'borrow>) -> Self::Output<'borrow>;
}

```

These traits could be specified by the syntax

```rust
FnBorrow([Args]<'borrow>) -> [Output]<'borrow>
FnBorrowMut([Args]<'borrow>) -> [Output]<'borrow>

```

where `[Args]` and `[Output]` work like with `Fn*` traits, but now admit a special lifetime `'borrow` to reference the lifetime of `'self`.

We might create types satisfying these traits with the familiar `|..| ...` syntax, which now satisfies both `Fn*` and `FnBorrow*` traits when possible, but may only satisfy the `FnBorrow*` variant. Is this workable? I think so, but it’s tricky:

If `v : &'a mut [T]` then `|| v.iter_mut()` satisfies `FnBorrowMut() -> IterMut<'borrow,T>`, but also satisfies `FnOnce() -> IterMut<'a,T>`. That’s okay, but we might want a `borrow |..| ...` syntax to distinguish, not sure.

---

<div class="post-metadata">

**Author:** ![JeffBurdges](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jeffburdges/32/2116_2.png) [@JeffBurdges](https://internals.rust-lang.org/u/JeffBurdges)\
**Post date:** [August 5, 2018, 7:29pm UTC](https://internals.rust-lang.org/t/fnborrow-traits/8136/2 "2018-08-05T19:29:01Z")

</div>

We might even magic a replacement for the `Borrow` traits

```rust
trait BorrowMut<T> = FnBorrowMut() -> &'borrow mut T;
trait Borrow<T> = FnBorrow() -> &'borrow T;

```

and permit ordinary values and referenced to be used with `FnBorrow*` traits.

There is an issue here that an `BorrowMut<T>` mutating `T` would receive considerable skepticism, but an `FnBorrowMut() -> &'borrow mut T` mutating the `T` looks less suspicious.

---

<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/fnborrow-traits/8136/3 "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.
