{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreidxtlefzp52fuixh4467po22myudzhumubnua4lnzvgminyl5vzxq",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mm7xsngnqph2"
},
"path": "/t/semantic-of-smart-pointers/24345#post_1",
"publishedAt": "2026-05-19T02:33:02.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"The Rust Programming Language",
"How to constrain types where `AsRef` and `AsMut` point to the same data? - help - The Rust Programming Language Forum",
"DerefPure"
],
"textContent": "\"Smart pointer\" is an individual concept in The Rust Programming Language book. However, I didn't find a semantic-level definition of what \"smart pointer\" is. Should there be some traits in core/std to provide this semantic?\n\nThis question arises from another question (How to constrain types where `AsRef` and `AsMut` point to the same data? - help - The Rust Programming Language Forum) I posted in the users forum:\n\n> When a function requires a smart pointer over a specific type, how to express this constraint?\n\nI guess this scenario is common? In my code, I often require a smart pointer to `[u8]` buffer, which enables me to access and modify the buffer's data (without appending or removing the buffer). Discussion of the problem: whether the code patterns that require a smart pointer is common, is also very welcome\n\nI was thinking of an `unsafe` trait (also suggested by others in the users forum post):\n\n\n /// # SAFETY\n ///\n /// Implementors should make sure the .as_ref() and .as_mut() returns the same data\n unsafe trait AsData<T>: AsRef<T> + AsMut<T> {}\n\n\nHowever, the correctness requirement in \"SAFETY\" section is still too weak to describe a smart pointer (which is also pointed out in the users forum post). The requirement only states that `as_ref` and `as_mut` return the same data, but there is no requirement of temporal consistency: the pointed data address should never change unless we deliberately replace it through `as_mut`. This is to prevent the implementors to randomly return different pointers in each invocations. I thought this is something like the DerefPure trait. This requirement is hard to express correctly, since what will happen if the smart pointer is a `Vec` and it reallocates?\n\nSo I suggest we shall discuss the semantic of smart pointers in Rust, and is it desirable to add some related trait to the core/std to express the semantic in type system?",
"title": "Semantic of smart pointers"
}