Skip to content

Commit

Permalink
removed constraints, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmcsherry committed May 28, 2018
1 parent 81034af commit 8724e7e
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/abomonated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ use super::{Abomonation, decode};
///
/// #Safety
///
/// The safety of this type, and in particular its `transute` implementation of
/// the `Deref` trait, relies on the owned bytes not being externally mutated
/// The safety of this type, and in particular its `transute` implementation of
/// the `Deref` trait, relies on the owned bytes not being externally mutated
/// once provided. You could imagine a new type implementing `DerefMut` as required,
/// but which also retains the ability (e.g. through `RefCell`) to mutate the bytes.
/// This would be very bad, but seems hard to prevent in the type system. Please
/// This would be very bad, but seems hard to prevent in the type system. Please
/// don't do this.
///
/// You must also use a type `S` whose bytes have a fixed location in memory.
/// Otherwise moving an instance of `Abomonated<T, S>` may invalidate decoded
/// pointers, and everything goes badly.
///
/// #Examples
///
/// ```
Expand All @@ -45,7 +49,7 @@ use super::{Abomonation, decode};
/// panic!("failed to decode");
/// }
/// ```
pub struct Abomonated<T: Abomonation, S: DerefMut<Target=[u8]>> {
pub struct Abomonated<T, S: DerefMut<Target=[u8]>> {
phantom: PhantomData<T>,
decoded: S,
}
Expand Down Expand Up @@ -83,6 +87,12 @@ impl<T: Abomonation, S: DerefMut<Target=[u8]>> Abomonated<T, S> {
/// panic!("failed to decode");
/// }
/// ```
///
/// #Safety
///
/// The type `S` must have its bytes at a fixed location, which will
/// not change if the `bytes: S` instance is moved. Good examples are
/// `Vec<u8>` whereas bad examples are `[u8; 16]`.
pub unsafe fn new(mut bytes: S) -> Option<Self> {

// performs the underlying pointer correction, indicates success.
Expand All @@ -100,7 +110,14 @@ impl<T: Abomonation, S: DerefMut<Target=[u8]>> Abomonated<T, S> {
}
}

impl<T: Abomonation, S: DerefMut<Target=[u8]>> Deref for Abomonated<T, S> {
impl<T, S: DerefMut<Target=[u8]>> Abomonated<T, S> {
pub fn as_bytes(&self) -> &[u8] {
&self.decoded
}
}


impl<T, S: DerefMut<Target=[u8]>> Deref for Abomonated<T, S> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
Expand Down

0 comments on commit 8724e7e

Please sign in to comment.