diff --git a/src/bytes.rs b/src/bytes.rs index c426f81..de12867 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -2,13 +2,13 @@ use {ByteBuf, SmallByteStr}; use traits::{Buf, ByteStr, ToBytes}; use std::{fmt, mem, ops, ptr}; use std::any::{Any, TypeId}; +use std::marker::Reflect; use std::raw::TraitObject; use core::nonzero::NonZero; const INLINE: usize = 1; /// A specialized `ByteStr` box. -#[unsafe_no_drop_flag] pub struct Bytes { vtable: NonZero, data: *mut (), @@ -21,7 +21,7 @@ impl Bytes { .unwrap_or_else(|| ByteBuf::from_slice(bytes).to_bytes()) } - pub fn of(bytes: B) -> Bytes { + pub fn of(bytes: B) -> Bytes { unsafe { if inline::() { let mut vtable; @@ -61,7 +61,7 @@ impl Bytes { /// If the underlying `ByteStr` is of type `B`, returns a reference to it /// otherwise None. - pub fn downcast_ref<'a, B: ByteStr + 'static>(&'a self) -> Option<&'a B> { + pub fn downcast_ref<'a, B: ByteStr>(&'a self) -> Option<&'a B> { if TypeId::of::() == self.obj().get_type_id() { unsafe { if inline::() { @@ -77,7 +77,7 @@ impl Bytes { /// If the underlying `ByteStr` is of type `B`, returns the unwraped value, /// otherwise, returns the original `Bytes` as `Err`. - pub fn try_unwrap(self) -> Result { + pub fn try_unwrap(self) -> Result { if TypeId::of::() == self.obj().get_type_id() { unsafe { // Underlying ByteStr value is of the correct type. Unwrap it @@ -137,7 +137,7 @@ impl ByteStr for Bytes { self.obj().buf() } - fn concat(&self, other: &B) -> Bytes { + fn concat(&self, other: &B) -> Bytes { self.obj().concat(&Bytes::of(other.clone())) } @@ -182,13 +182,10 @@ impl Clone for Bytes { impl Drop for Bytes { fn drop(&mut self) { - if *self.vtable == 0 { - return; - } - unsafe { if self.is_inline() { - self.obj_mut().drop(); + let obj = self.obj_mut(); + obj.drop(); } else { let _: Box = mem::transmute(self.obj()); @@ -221,7 +218,7 @@ trait ByteStrPriv { fn split_at(&self, mid: usize) -> (Bytes, Bytes); } -impl ByteStrPriv for B { +impl ByteStrPriv for B { fn buf(&self) -> Box { Box::new(self.buf()) @@ -264,7 +261,8 @@ impl ByteStrPriv for B { #[test] pub fn test_size_of() { - let expect = mem::size_of::() * 2; + // TODO: One day, there shouldn't be a drop flag + let expect = mem::size_of::() * 3; assert_eq!(expect, mem::size_of::()); assert_eq!(expect, mem::size_of::>()); diff --git a/src/lib.rs b/src/lib.rs index ca7c391..3877d68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![crate_name = "bytes"] #![unstable] -#![feature(alloc, convert, core, unsafe_no_drop_flag)] +#![feature(alloc, convert, core)] pub use byte_buf::{ByteBuf, ROByteBuf, MutByteBuf}; pub use byte_str::{SeqByteStr, SmallByteStr, SmallByteStrBuf}; @@ -11,6 +11,7 @@ pub use rope::{Rope, RopeBuf}; pub use slice::{SliceBuf, MutSliceBuf}; use std::{cmp, fmt, io, ops, ptr, u32}; +use std::marker::Reflect; extern crate core; @@ -198,7 +199,7 @@ pub trait MutBufExt { /// An immutable sequence of bytes. Operations will not mutate the original /// value. Since only immutable access is permitted, operations do not require /// copying (though, sometimes copying will happen as an optimization). -pub trait ByteStr : Clone + Sized + Send + Sync + ToBytes + ops::Index { +pub trait ByteStr : Clone + Sized + Send + Sync + Reflect + ToBytes + ops::Index + 'static { // Until HKT lands, the buf must be bound by 'static type Buf: Buf+'static; @@ -382,7 +383,7 @@ impl<'a> Sink for &'a mut Vec { unsafe { { - let dst = self.as_mut_slice(); + let dst = &mut self[..]; let cnt = buf.read_slice(slice::from_raw_parts_mut(dst.as_mut_ptr(), rem)); debug_assert!(cnt == rem);