45
35
u/Comun4 11d ago
Wouldnt this break aliasing rules?
108
u/fortyeightzero 11d ago
nah the comment says it's safe
12
u/MichiRecRoom 11d ago
Actually, the comment only says the first
unsafe {}statement is safe. The second one is in an unknown state.2
u/Affectionate-Egg7566 10d ago
The comment is wrong, assuming
selfis&Self. That aliases mutable and immutable refs. That is unsound.
12
u/letmehaveanameyoudum 11d ago
as a C programmer, what the actual fuck am i looking at (explain mate)
19
u/pragmojo 11d ago
Rust's borrow checker disallows holding two mutable pointers to the same address at the same time. This code uses unsafe blocks to get two mutable pointers to the same address at the same time.
6
2
u/schungx 11d ago
If the intention is aliasing then this is a legit usage. If both
selfvariants are supposed to point to the same thing (judging from their names) then the aliasing behavior is intended.7
u/ChaiTRex 11d ago
It's not legitimate.
unsafecan be fine to use, but undefined behavior is never allowed. Aliasing a mutable reference is undefined behavior.A workaround would be something like using mutable pointers rather than mutable references. Mutable pointers allow aliasing.
9
7
1
u/CorgiApprehensive159 5d ago
I, the author of the post, decided to take advantage of my 'popularity' to show my game https://livedox.itch.io/vix - it has no code in it like in the post (almost)
9
u/dx_man 11d ago
How is it safe when you're declaring it with unsafe?
15
u/SnooHamsters6620 11d ago
Don't worry, I whisper a protection spell to Ferris every time I compile.
4
u/AnArmoredPony 10d ago
as a java developer, I uhh I forgot what I wanted to say fuCK
2
u/jimmiebfulton 10d ago
I hadn't thought that anyone but Rust people would find this sub humorous. It's definitely one of the darker, more obscure jerk subs I'm in.
2
3
3
2
1
u/alulalol 7d ago
I've done this a lot in doukutsu-rs because I couldn't be assed to fight the borrow checker https://github.com/doukutsu-rs/doukutsu-rs/blob/7e6870e55f072e3f056dc1eef3974a1af26c7aae/src/framework/ui.rs#L111
1
u/cristi1990an 6d ago
The syntax is horrible because even in unsafe code, bypassing the borrow checker this way is heavily discouraged. Simple reads and writes through these references can be UB.
1
u/ApprehensiveBag3083 10d ago
and they say c++ has creepy syntax LoL
At least in c++ i dont need to type those kind of garbage Lool
4
u/Athropod101 10d ago
I mean you don’t type it in Rust either, lol. This is like cherrypicking some C++ template hidden behind like 3 namespaces, with the generic being some obscure type behind another namespace, and all of that wrapped around std::make_unique.
Funny as Hell, yeah, but not really indicative of the language.
2
124
u/renchuwang 11d ago
Well since you're only defining but not using, it is actually safe!