]> git.lizzy.rs Git - rust.git/commitdiff
Make std::raw::Repr an unsafe trait
authorUlrik Sverdrup <root@localhost>
Fri, 13 Feb 2015 19:31:09 +0000 (20:31 +0100)
committerHuon Wilson <dbau.pp+github@gmail.com>
Sat, 14 Feb 2015 00:32:42 +0000 (11:32 +1100)
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]

src/libcore/raw.rs

index 81edfe7d6e268d43688e671d3827123bd40ace8b..bb0c22d3561d26138a8b6cadd49e8fb6a4be2d34 100644 (file)
@@ -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
@@ -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 {}