| ▲ | shadowgovt 4 hours ago | |||||||||||||
Is there a similar solution to doing this in Rust? I suppose inside `unsafe` you can do basically anything. | ||||||||||||||
| ▲ | tialaramex 3 hours ago | parent | next [-] | |||||||||||||
Unlike C++ all of Rust's primitive types get the same first class treatment as your user defined types and so the appropriate API is provided as methods on pointer types. For this you want ptr::map_addr which takes a callable (such as your own function for this mapping or a lambda) to fiddle with the pointer. https://doc.rust-lang.org/std/primitive.pointer.html#method.... Rust's MIRI is able to run code which uses this (a strict provenance API) because although MIRI's pointers are some mysterious internal type, it can track that we mapped them to hide our tags, and then later mapped back from the tagged pointer to recover our "real" pointer and see that's fine. This isn't an unsafe operation. Dereferencing a pointer is unsafe, but twiddling the bits is fine, it just means whoever writes the unsafe dereferencing part of your codebase needs to be very careful about these pointers e.g. making sure the ones you've smuggled a tag in aren't dereferenced 'cos that's Undefined Behaviour. It's clear to me how this works in Rust, it's just unclear still in C++ | ||||||||||||||
| ▲ | trws 3 hours ago | parent | prev | next [-] | |||||||||||||
Everything else in the siblings is true, but remember that the language and std types in rust all do this already. Most of the time it’s better to use a native enum or optional/result because they do this in the compiler/lib. It’s only really worth it if you need more than a few types or need precise control of the representation for C interop or something. | ||||||||||||||
| ||||||||||||||
| ▲ | simonask 3 hours ago | parent | prev | next [-] | |||||||||||||
Rust is basically in the same place as C++, i.e. provenance rules are currently ad-hoc/conventional, meaning that pointer tagging is a grey area. | ||||||||||||||
| ||||||||||||||
| ▲ | thecloudlet 4 hours ago | parent | prev [-] | |||||||||||||
Waiting for Rust experts. | ||||||||||||||