Skip to content

std::raw::Repr is not safe to implement #22260

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

Closed
brson opened this issue Feb 13, 2015 · 1 comment
Closed

std::raw::Repr is not safe to implement #22260

brson opened this issue Feb 13, 2015 · 1 comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@brson
Copy link
Contributor

brson commented Feb 13, 2015

Here's the definition

/// This trait is meant to map equivalences between raw structs and their
/// corresponding rust values.
pub trait Repr<T> {
    /// This function "unwraps" a rust value (without consuming it) into its raw
    /// struct representation. This can be used to read/write different values
    /// for the struct. This is a safe method because by default it does not
    /// enable write-access to the fields of the return value in safe code.
    #[inline]
    fn repr(&self) -> T { unsafe { mem::transmute_copy(&self) } }
}

Implementing is done like so:

impl<T> Repr<Slice<T>> for [T] {}

A safe implementation invokes a transmute between arbitrary types. Perhaps Repr is more appropriately an unsafe trait?

@brson brson added the A-libs label Feb 13, 2015
@Gankra Gankra added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Feb 13, 2015
@Gankra
Copy link
Contributor

Gankra commented Feb 13, 2015

Agreed. Unsafe trait is the way to go.

bluss pushed a commit to bluss/rust that referenced this issue Feb 13, 2015
The default implementation of .repr() will call conveniently call
transmute_copy which should be appropriate for all implementors, but is
memory unsafe if used wrong.

Fixes rust-lang#22260

You need to use `unsafe impl` to implement the Repr trait now.

[breaking-change]
@huonw huonw closed this as completed in 7a52932 Feb 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

2 participants