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