« first day (3090 days earlier)      last day (386 days later) » 

1:39 PM
Why does my Deref impl not work with tuple structs when it's defined at the same module where it's used and not inside a subordinate mod? play.rust-lang.org/…
 
 
1 hour later…
2:49 PM
@purefanatic weird, but deref is always a little magic for me even after so many years with rust
 
Deref is a footgun
 
@purefanatic Dunno but your code includes UB (creating a mutable reference to an immutable memory location) so all bets are off anyways.
 
@cafce25 How so? I only create a mutable pointer, no?
 
3:05 PM
What do you think unsafe {&mut* self.0} does in deref_mut?
 
It creates a mutable reference to a mutable memory location?
 
Not if you created the pointer from an immutable reference, like say for example a static string slice literal.
Arggh never mind me, I can't read code today it seems.
 
3:22 PM
;-)
 
3:37 PM
@purefanatic Why would .0 use the deref implementation when the type you're calling it on has a perfectly accessible .0 already? The compiler isn't going to do complex analysis or backtrack to figure out your ambiguous use-case.
 
Hm. Why wouldn't it? That's what Deref is made for, no? The same stuff happens if MyBox is not a tuple struct but has a field that also exists in T fwiw.
 
Deref will only kick in if the method or field isn't available on the type itself. Deref is the fallback, its not the first place the compiler looks.
 
3:54 PM
Well obviously that's not always the case... The point of my question was to find out under which circumstances exactly so that I can avoid these!
 
I'm confused, you already found a solution by putting it in a module. Modules are a unit of visibility so any private items or fields in it won't be accessible outside of it.
If you do want to provide methods for your deref type, then you should avoid taking self so you are forced to use the MyBox::method(b) syntax, otherwise you can needlessly shadow method calls to the referenced type. Look at how Box::leak does its signature.
 
4:24 PM
honestly only a compiler expert could answer your question
 
4:36 PM
@kmdreko Ah, this makes more sense. I have not quite thought about the visibility. I guess this is the reason at least in my example and I need to find another place for my tests than alongside the module to test (this is where I discovered the issue originally).
I have another question... Is there any reliable solution to stack overflows in debug mode when creating Boxes of large types? In this thread: users.rust-lang.org/t/… someone suggested the "copyless" crate but it does not seem work anymore...
 
I though this problem was fixed
 
Nah... I just found this issue and am a little hopeless after looking at the last comment :/ github.com/rust-lang/rust/issues/53827
 
anyway just use a vector
 
4:52 PM
The only surefire way is never to create a value of the type on the stack: no variable, no temporary. In practice, this means calling the allocator, getting raw memory, and initializing that. If you were thinking of a large, Vec (and possibly decaying it to Box<[T]>) works pretty well. If have a large type which you can't modify, then you'll have to do it manually...
 
Yeah, but the crux is the last part, initializing. I have a type like struct X<T> { data: [MaybeUninit<T>; 128], free: u128}. Rust does not have any placement new syntax like C++. But I guess in my example I can probably find out how to initialize the free field correctly, just need to get a little more accustomed to unsafe land...
 
I think that the most unsafe thing I ever write in Rust
 
@Stargateur Looks nice :D Probably this is all I need! Or maybe I'll actually just use a vector like you suggested
 
vector is good :p
 
5:20 PM
Here's Stargateur's example modified to use X<T> and with safety comments justifying why the unsafe parts are sound: play.rust-lang.org/…
I do note that it would be a lot easier if instead of free, you had used as a field, as then 0 would be the correct bit pattern from the get-go, which would mean that alloc_zeroed directly provides suitably initialized memory.
I also note that for more complex types it gets much trickier, as there is no easy way to get a &mut MaybeUninit<F> pointing to a field of a &mut MaybeUninit<T> at the moment, meaning you need to take the raw pointer to MaybeUninit<T>, cast it to bytes, apply a suitable offset, cast it to a raw pointer to MaybeUninit<F>, and finally initialize F.
So I really advise having all 0s as a valid state. Ideally, as the default state, as well, but even if not, it's easier if you can just alloc_zeroed and deference the pointer without having to juggle MaybeUninit and pointer offsets. This does mean not having non MaybeUninit wrapped enums and bools, notably.
 
This makes a lot of sense. Thanks all of you for your great efforts!
 

« first day (3090 days earlier)      last day (386 days later) »