Skip to content

Is repr(transparent) completely transparent within repr(Rust) types? #298

Closed
@Manishearth

Description

@Manishearth

repr(transparent) exists which makes it safe to cast references between e.g. MaybeUninit<T> and T

Does this extend to repr(Rust) types containing repr(transparent)? I.e., are we guaranteed that the following pairs of types:

struct Struct {
  x: String,
  y: Foo
}
struct StructUninit {
  x: String,
  y: MaybeUninit<Foo>
}

enum Enum {
   A(String),
   B(Foo, String)
}
enum EnumUninit {
   A(String),
   B(MaybeUninit<Foo>, String)
}

are fully layout compatible? The primary guarantee of repr(transparent) is that it does not affect the size and alignment, but my understanding is that at the moment repr(Rust) has no guarantees; it would be legal for repr(Rust) to say "add a padding of 2 in every field whose type starts with an 'M'".

This guarantee would be very nice to have; because otherwise there's no way to do in-place initialization of Rust enums without repr(C).

cc @rust-lang/lang (unsure if this is the right place to file this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions