{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreic7xegpck7o7jie335vzy3hxv22auvahim2bugomtb3bbovy3ljs4",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mp4mv7hlwqf2"
},
"path": "/t/a-read-only-no-alias-reference/24410#post_12",
"publishedAt": "2026-06-24T17:55:14.000Z",
"site": "https://internals.rust-lang.org",
"textContent": "It is already possible to do that:\n\n\n pub struct ExclusiveReadOnly<'a, T: ?Sized>(&'a mut T);\n\n impl<'a, T: ?Sized> ExclusiveReadOnly<'a, T> {\n pub fn new(r: &'a mut T) -> Self {\n Self(r)\n }\n }\n\n impl<T: ?Sized> std::ops::Deref for ExclusiveReadOnly<'_, T> {\n type Target = T;\n\n fn deref(&self) -> &T {\n self.0\n }\n }\n\n\nCompiler optimizations will treat this struct like `&mut T`, but it's impossible to write into it.\n\nHowever this doesn't enable any additional optimizations, in fact it enables less: `&T` is already `noalias`, and this struct will not be `readonly`. `noalias` in LLVM means that if there are writes to this memory, they will be done from this pointer only. If there are no writes, this is trivially satisfied. LLVM just doesn't have any attribute for \"readonly exclusive pointer\", and I doubt such attribute has any optimization utility. Furthermore, the current memory models proposed for Rust also have no such thing, so it will require extending them for very dubious benefit.",
"title": "A read only no alias reference"
}