From 85f48d300a1161338e14b9579007b003c48dc4ba Mon Sep 17 00:00:00 2001 From: Amit Aryeh Levy Date: Sat, 19 Sep 2015 15:33:34 -0400 Subject: [PATCH 1/2] Make FixedSizeArray an unsafe trait --- src/libcore/array.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/array.rs b/src/libcore/array.rs index 8c785b109236d..976689beccd11 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -35,14 +35,14 @@ use slice::{Iter, IterMut, SliceExt}; /// /// This trait can be used to implement other traits on fixed-size arrays /// without causing much metadata bloat. -pub trait FixedSizeArray { +pub unsafe trait FixedSizeArray { /// Converts the array to immutable slice fn as_slice(&self) -> &[T]; /// Converts the array to mutable slice fn as_mut_slice(&mut self) -> &mut [T]; } -impl> FixedSizeArray for A { +unsafe impl> FixedSizeArray for A { #[inline] fn as_slice(&self) -> &[T] { self From b30d8969e86fa2c9dd3b8e2e28ddda2202331f0f Mon Sep 17 00:00:00 2001 From: Amit Aryeh Levy Date: Wed, 23 Sep 2015 11:38:01 -0400 Subject: [PATCH 2/2] Explain in comment why FixedSizeArray is unsafe --- src/libcore/array.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcore/array.rs b/src/libcore/array.rs index 976689beccd11..c986914440de0 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -35,6 +35,15 @@ use slice::{Iter, IterMut, SliceExt}; /// /// This trait can be used to implement other traits on fixed-size arrays /// without causing much metadata bloat. +/// +/// The trait is marked unsafe in order to restrict implementors to fixed-size +/// arrays. User of this trait can assume that implementors have the exact +/// layout in memory of a fixed size array (for example, for unsafe +/// initialization). +/// +/// Note that the traits AsRef and AsMut provide similar methods for types that +/// may not be fixed-size arrays. Implementors should prefer those traits +/// instead. pub unsafe trait FixedSizeArray { /// Converts the array to immutable slice fn as_slice(&self) -> &[T];