Skip to content

Commit

Permalink
Make std::raw::Repr an unsafe trait
Browse files Browse the repository at this point in the history
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 #22260

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

[breaking-change]
  • Loading branch information
Ulrik Sverdrup authored and huonw committed Feb 14, 2015
1 parent 5e3ae10 commit 7a52932
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/libcore/raw.rs
Expand Up @@ -154,7 +154,7 @@ pub struct TraitObject {

/// This trait is meant to map equivalences between raw structs and their
/// corresponding rust values.
pub trait Repr<T> {
pub unsafe 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
Expand All @@ -163,5 +163,5 @@ pub trait Repr<T> {
fn repr(&self) -> T { unsafe { mem::transmute_copy(&self) } }
}

impl<T> Repr<Slice<T>> for [T] {}
impl Repr<Slice<u8>> for str {}
unsafe impl<T> Repr<Slice<T>> for [T] {}
unsafe impl Repr<Slice<u8>> for str {}

0 comments on commit 7a52932

Please sign in to comment.