{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihwjkcqopr25gx5vbjtmbftvn6cukjm6tbydmjozg4vs6vekfefd4",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mlynjdd37xq2"
},
"path": "/t/reducing-raw-pointer-footguns-preventing-reference-aliasing-violations-at-compile-time/24301#post_19",
"publishedAt": "2026-05-16T08:29:08.000Z",
"site": "https://internals.rust-lang.org",
"textContent": "The purpose of `AliasingGuard` is to wrap raw pointer usage before the raw pointer is actually used, then using with_mutable_pointer for the operation that use pointer\n\nNot like this:\n\n\n let mut leaked: *mut i32 = std::ptr::null_mut();\n\n guard.with_mutable_pointer(|ptr| {\n leaked = ptr;\n });\n\n let r = guard.mutable_reference();\n\n unsafe {\n *leaked = 123;\n }\n\n\nBut like this:\n\n\n let mut leaked: *mut i32 = std::ptr::null_mut();\n\n guard.with_mutable_pointer(|ptr| {\n leaked = ptr;\n });\n\n let r = guard.mutable_reference();\n\n guard.with_mutable_pointer(|ptr| {\n *ptr = 3;\n });\n\n\nBecause once we wrap it, we opt into routing pointer operations through the guard whenever possible, which should cover the majority of cases\n\nMaybe you could show examples of code that genuinely can not be expressed through the guard API, so we can investigate whether the code can be improved further. That way, we can gradually build a stronger safety around raw pointer usage. For now I'm trying to add sub guard :]\n\nMeanwhile, `unsafe as_ptr()` exists to revert back to the unrestricted/raw style without the guard managing aliasing anymore. In other words, the guard explicitly steps aside because we requested to turn off the guard system entirely by escaping the pointer. It exists to support edge cases where `with_mutable_pointer()` is insufficient that is also not discovered yet where `with_mutable_pointer()` is not sufficient",
"title": "Reducing Raw Pointer Footguns: Preventing Reference Aliasing Violations at Compile Time"
}