Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RevSlice requires repr(C) or repr(transparent) #1

Closed
dtolnay opened this issue Oct 5, 2018 · 2 comments
Closed

RevSlice requires repr(C) or repr(transparent) #1

dtolnay opened this issue Oct 5, 2018 · 2 comments

Comments

@dtolnay
Copy link

dtolnay commented Oct 5, 2018

Without one of those attributes, my understanding is the transmute from &[T] to &RevSlice<T> is undefined behavior. The compiler is free to stick some additional data at the start of RevSlice<T> or give it a stricter alignment than [T]. For example some targets may require all struct types to have some minimum alignment that is not required for slices.

From nomicon's Alternative representations:

#[repr(C)] is also necessary to soundly do more elaborate tricks with data layout such as reinterpreting values as a different type.

@scottmcm
Copy link
Owner

scottmcm commented Oct 5, 2018

Yes, that's theoretically true right now, but it may very soon not be: rust-lang/unsafe-code-guidelines#31 (comment)

@scottmcm
Copy link
Owner

scottmcm commented Oct 22, 2018

Made it transparent in 0.1.4.

rev_slice/src/lib.rs

Lines 65 to 68 in b555bcd

/// A DST newtype providing a reversed view of a slice.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct RevSlice<T>([T]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants