# Improving self-referential structs

**URL:** <https://internals.rust-lang.org/t/improving-self-referential-structs/4808>\
**Category:** language design\
**Created:** [February 18, 2017, 1:07am UTC](https://internals.rust-lang.org/t/improving-self-referential-structs/4808 "2017-02-18T01:07:43Z")\
**Posts on this page:** 4\
**Page:** 5

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [November 1, 2018, 3:56am UTC](https://internals.rust-lang.org/t/improving-self-referential-structs/4808/82 "2018-11-01T03:56:23Z")

</div>

> [@philipc](#):
>
> Would it need to verify it is a character boundary too?

True because its strings, I still doubt this is worth dropping into unsafe self references.

---

<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:** [November 1, 2018, 3:53pm UTC](https://internals.rust-lang.org/t/improving-self-referential-structs/4808/83 "2018-11-01T15:53:56Z")

</div>

Transmute without turbofish is pretty scary, since it could easily be UB. I’m guessing you’re widening a lifetime here? You should covert it into a `Box<str>` with `into_boxed_str()` to pin it in the heap, if that’s the case.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [November 3, 2018, 11:34pm UTC](https://internals.rust-lang.org/t/improving-self-referential-structs/4808/84 "2018-11-03T23:34:04Z")

</div>

> [@djc](#):
>
> Another solution is to apply a small smattering of `unsafe`, as is done here:
> 
> [https://github.com/djc/tokio-imap/blob/master/tokio-imap/src/proto.rs#L38](https://github.com/djc/tokio-imap/blob/master/tokio-imap/src/proto.rs#L38)

_Uhhhhhhhhhhhh._

> [@philipc](#):
>
> I don’t know why tokio-imap made the `response` member public, seems dangerous.

Okay, thank goodness _at least one_ other person finds this hair-raising.

Here is UB in `tokio_imap`:

```rust
extern crate tokio_imap;
extern crate tokio_codec;
extern crate bytes;

use std::fmt::Debug;
use tokio_codec::Decoder;
use tokio_imap::proto::{ImapCodec};

// Returns Response<'static>
fn make_bomb() -> impl Debug {
    use bytes::{BytesMut, BufMut};
    let mut buf = BytesMut::with_capacity(1_000_000_000);
    buf.put("* OK [UNSEEN 3] Message 3 is first unseen\r\n");

    let mut decoder = ImapCodec::default();
    let mut result = decoder.decode(&mut buf).unwrap().unwrap();

    println!("{:?}", result.response); // seems legit
    result.response
} // result is freed. uh oh.

fn main() {
    let bomb = make_bomb();
    for _ in 0..10 {
        // make other allocations that zero out memory
        make_bomb();
    }
    println!("{:?}", bomb);
}

```

**Release output:**

```rust
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("Message 3 is first unseen") }
Data { status: Ok, code: Some(Unseen(3)), information: Some("\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}") }

```

* * *

**Edit:** created [undefined behavior in safe code with public Response\<'static\> field · Issue #30 · djc/tokio-imap · GitHub](https://github.com/djc/tokio-imap/issues/30)

---

<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:27am UTC](https://internals.rust-lang.org/t/improving-self-referential-structs/4808/85 "2019-03-25T08:27:58Z")

</div>

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

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

[Previous page](https://internals.rust-lang.org/t/improving-self-referential-structs/4808.md?page=4)
