Skip to content

Commit

Permalink
Revert "Add Default bound to Clearable"
Browse files Browse the repository at this point in the history
I feel supporting fixed length arrays is more important right now.
Rust might have type level numerics and Default for fixed lenght
arrays before it gets a garbage collector or other dangerous types
based on `Shared<T>`.

This reverts commit ccb563e.
  • Loading branch information
burdges committed Jan 16, 2017
1 parent ccb563e commit 88b4c4f
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/clearable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// Types that can safely be dropped after first being overwritten by zeros.
///
/// There is a default implementation for all `Copy+Default` types and all
/// unsized arrays of `Clerable` types. You need to implement `Clerable`
/// yourself for unsized structs, or if your type could be `Copy+Default`
/// but you do not want it to be.
/// There is a default implementation for all `Copy` types and all unsized
/// arrays of `Clerable` types. You need to implement `Clerable` yourself
/// for unsized structs, or if your type could be `Copy` but you do not
/// want it to be.
///
/// ```
/// # use clear_on_drop::Clearable;
Expand All @@ -15,32 +15,26 @@
/// }
/// ```
///
/// We need `Copy` to prevent pointer types like `&mut T` `Box<T>`,
/// `Arc<T>`, etc. from being `Clearable`. We need `Default` bound only
/// because `Shared<T>` is `Copy`. At present, `Shared<T>` is the only
/// "bad" `Copy` type, but future abstractions built using `Shared<T>`
/// might be `Copy`, including perhaps garbage collected abstractions.
/// Using these could result in memory leaks or calling `drop` with a
/// null pointer.
/// Warning: `Shared<T>` is `Copy` and hence `Clearable`. At present, no
/// other pointer types are `Copy`, but future abstractions built using
/// `Shared<T>` might be `Copy` and hence `Clearable`, including perhaps
/// garbage collected abstractions. Using these could result in memory
/// leaks or calling `drop` with a null pointer.
pub unsafe trait Clearable {
/// Clear data by dropping it and overwriting it with fixed data,
/// possibly leaving in an unusable state. The object must be
/// safe to drop again after being overwritten with zeros however.
/// Clear data by dropping it and overwriting it with zeros,
/// possibly leaving in an unusable state. The object must
/// be safe to drop again after being overwritten with zeros.
unsafe fn clear(&mut self);
}

unsafe impl<T> Clearable for T where T: Copy+Default {
unsafe impl<T> Clearable for T where T: Copy {
unsafe fn clear(&mut self) {
*self = ::std::mem::zeroed::<Self>();
// Assigning like this is equivelent to
// ::std::ptr::drop_in_place::<Self>(self);
// ::std::ptr::write_unaligned::<T>(self, ::std::mem::zeroed::<Self>())
// because the safety notes on ptr::read say it drops the value
// previously at *self.
::std::ptr::write::<Self>(self, Default::default());
// Should this be ::std::ptr::write_unaligned?
// see https://github.com/rust-lang/rust/issues/37955
}
}

Expand Down

0 comments on commit 88b4c4f

Please sign in to comment.