# Supporting 'janitorial' style RAII

**URL:** <https://internals.rust-lang.org/t/supporting-janitorial-style-raii/12190>\
**Category:** language design\
**Created:** [April 18, 2020, 7:13pm UTC](https://internals.rust-lang.org/t/supporting-janitorial-style-raii/12190 "2020-04-18T19:13:30Z")\
**Posts on this page:** 1\
**Showing post:** 29

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 18, 2020, 11:34pm UTC](https://internals.rust-lang.org/t/supporting-janitorial-style-raii/12190/29 "2020-04-18T23:34:06Z")

</div>

As far as I can tell, the idea behind this whole thread still being alive is that instead of the already proposed approach

```rust
let x: &mut Something = //...
// do sth
{
    let guard = specialized_guard(x);
    let x = guard.as_mut();
    // use inner x
}
// use outer x again

```

which could (as mentioned) be shortened with a macro, and which unfortunately doesn’t quite work with `self` syntactically, there ought to be another way. The proposed approach is not “nice” enough, reason being: You need a local copy of `x` there which is essentially the same as the outer `x`, but you know it isn’t _really_ the same `x` and there _ought to be (and if not change the language)_ a way to instead write something like

```rust
let x: &mut Something = //...
// do sth
{
    let guard = specialized_guard(&mut#reborrow_delayed *x);
    // use outer x
}
// use outer x 

```

Unfortunately using the same outer `x` is _not_ safe. The `x`’s are not the same in the following regard: The inner `x` has a shorter life-time and this fact _is significant_. As demonstrated for example with this code:

```rust
use crossbeam_utils::thread;

let x: &mut Something = //...
// do sth
thread::scope(|s| {
    // do sth more
    {
        let guard = specialized_guard(&mut#reborrow_delayed *x);
        let _handle = s.spawn(|_| /* use outer x */);
        // can’t use outer x here anymore
    }
    // can’t use outer x here anymore
}).unwrap();
// can use outer x again

```

Here, all the sudden, a different thread could access `x` mutably _in parrallel_ with the guard being dropped. This kind of code would be rejected with a distinct, shorter-lived inner `x`, since the spawned scoped thread can only be passed references with a lifetime of at least the scope `s`.

---

_[View the full topic](https://internals.rust-lang.org/t/supporting-janitorial-style-raii/12190)._
