These are chat archives for rust-lang/rust

28th
Jan 2016
Igor
@target-san
Jan 28 2016 12:50

What if I have struct NewType(SomeOtherType);, will

    let value: SomeOtherType = SomeOtherType::new(...);
    let proxy_ref: &NewType = unsafe { std::mem::transmute(&value) };

be Ok?

Jarred Nicholls
@jnicholls
Jan 28 2016 12:51
Lost part of that message it seems @target-san
Jarred Nicholls
@jnicholls
Jan 28 2016 12:56
@target-san I'm not sure that will compile actually. The memory is laid out the same in theory, but pretty sure transmute will not like that?
Maybe it will, same size
Igor
@target-san
Jan 28 2016 12:57
Checked in Playground, seems to be Ok
Vladimir Matveev
@netvl
Jan 28 2016 12:59
@target-san AFAIK Rust does not define struct layout, so it is unsafe to do this. I can image that drop flags may break this code
Jarred Nicholls
@jnicholls
Jan 28 2016 12:59
Impl Drop on NewType breaks it for sure
Vladimir Matveev
@netvl
Jan 28 2016 13:00
@jnicholls this is pointers transmutation, it will compile. BTW, I suppose pointer conversions would be more idiomatic
unsafe { &*(&value as *const _ as *const NewType) }
Jarred Nicholls
@jnicholls
Jan 28 2016 13:01
Are you trying to avoid an expensive move or something?
I wrap types and impl Deref and DerefMut for ergonomic use.
In short I don't understand why one would upcast to a new type struct
Igor
@target-san
Jan 28 2016 13:03
It's about serde crate, specifically recent proposal on proxy types. The problem is, you need separate proxy types for serialization and deserialization
because serialization takes reference, but deserialization produces value.
Jarred Nicholls
@jnicholls
Jan 28 2016 13:04
I'm very familiar with serde...link to proposal?
Igor
@target-san
Jan 28 2016 13:04
serde-rs/serde#198
Jarred Nicholls
@jnicholls
Jan 28 2016 13:05
Thx
Igor
@target-san
Jan 28 2016 13:05
It's conflicting now though
To sum up, two proxy types there can be unified into one either by requiring COW-like thing inside, or using ref transmutation I'm talking about
Jarred Nicholls
@jnicholls
Jan 28 2016 13:27
Roger
Denis Lisov
@tanriol
Jan 28 2016 13:53
What are the current best practices wrt -sys crates? Are they commonly expected to be bindgen-generated with only the minimum changes to make it compile?
Peter Atashian
@retep998
Jan 28 2016 15:25
@tanriol While bindgen is an efficient way to produce the bindings for -sys crates, it is by no means necessary, and I prefer to not use it as it doesn't support things like macro constants among other issues I have with it
@tanriol In the end the -sys crates are supposed to just be raw unsafe bindings to the functions/types/constants with minimal glue.
Jarred Nicholls
@jnicholls
Jan 28 2016 19:41
@tanriol I tend to use bindgen via a build.rs workflow for most libraries, but sometimes it is best to write the bindings by hand, particularly if it is your own library or it is on the smaller side. Wrapping bindgen'ed output can be tedious sometimes.