Skip to content

Commit 7a52932

Browse files
Ulrik Sverdruphuonw
Ulrik Sverdrup
authored andcommitted
Make std::raw::Repr an unsafe trait
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]
1 parent 5e3ae10 commit 7a52932

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libcore/raw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub struct TraitObject {
154154

155155
/// This trait is meant to map equivalences between raw structs and their
156156
/// corresponding rust values.
157-
pub trait Repr<T> {
157+
pub unsafe trait Repr<T> {
158158
/// This function "unwraps" a rust value (without consuming it) into its raw
159159
/// struct representation. This can be used to read/write different values
160160
/// for the struct. This is a safe method because by default it does not
@@ -163,5 +163,5 @@ pub trait Repr<T> {
163163
fn repr(&self) -> T { unsafe { mem::transmute_copy(&self) } }
164164
}
165165

166-
impl<T> Repr<Slice<T>> for [T] {}
167-
impl Repr<Slice<u8>> for str {}
166+
unsafe impl<T> Repr<Slice<T>> for [T] {}
167+
unsafe impl Repr<Slice<u8>> for str {}

0 commit comments

Comments
 (0)