Skip to content

Commit 94c543f

Browse files
authored
remove ext traits (#431)
1 parent 447530b commit 94c543f

File tree

12 files changed

+199
-209
lines changed

12 files changed

+199
-209
lines changed

src/buf/buf_impl.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#[cfg(feature = "std")]
2+
use crate::buf::{reader, Reader};
3+
use crate::buf::{take, Chain, Take};
4+
15
use core::{cmp, mem, ptr};
26

37
#[cfg(feature = "std")]
@@ -807,6 +811,87 @@ pub trait Buf {
807811
ret.put(self);
808812
ret.freeze()
809813
}
814+
815+
/// Creates an adaptor which will read at most `limit` bytes from `self`.
816+
///
817+
/// This function returns a new instance of `Buf` which will read at most
818+
/// `limit` bytes.
819+
///
820+
/// # Examples
821+
///
822+
/// ```
823+
/// use bytes::{Buf, BufMut};
824+
///
825+
/// let mut buf = b"hello world"[..].take(5);
826+
/// let mut dst = vec![];
827+
///
828+
/// dst.put(&mut buf);
829+
/// assert_eq!(dst, b"hello");
830+
///
831+
/// let mut buf = buf.into_inner();
832+
/// dst.clear();
833+
/// dst.put(&mut buf);
834+
/// assert_eq!(dst, b" world");
835+
/// ```
836+
fn take(self, limit: usize) -> Take<Self>
837+
where
838+
Self: Sized,
839+
{
840+
take::new(self, limit)
841+
}
842+
843+
/// Creates an adaptor which will chain this buffer with another.
844+
///
845+
/// The returned `Buf` instance will first consume all bytes from `self`.
846+
/// Afterwards the output is equivalent to the output of next.
847+
///
848+
/// # Examples
849+
///
850+
/// ```
851+
/// use bytes::Buf;
852+
///
853+
/// let mut chain = b"hello "[..].chain(&b"world"[..]);
854+
///
855+
/// let full = chain.to_bytes();
856+
/// assert_eq!(full.bytes(), b"hello world");
857+
/// ```
858+
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
859+
where
860+
Self: Sized,
861+
{
862+
Chain::new(self, next)
863+
}
864+
865+
/// Creates an adaptor which implements the `Read` trait for `self`.
866+
///
867+
/// This function returns a new value which implements `Read` by adapting
868+
/// the `Read` trait functions to the `Buf` trait functions. Given that
869+
/// `Buf` operations are infallible, none of the `Read` functions will
870+
/// return with `Err`.
871+
///
872+
/// # Examples
873+
///
874+
/// ```
875+
/// use bytes::{Bytes, Buf};
876+
/// use std::io::Read;
877+
///
878+
/// let buf = Bytes::from("hello world");
879+
///
880+
/// let mut reader = buf.reader();
881+
/// let mut dst = [0; 1024];
882+
///
883+
/// let num = reader.read(&mut dst).unwrap();
884+
///
885+
/// assert_eq!(11, num);
886+
/// assert_eq!(&dst[..11], &b"hello world"[..]);
887+
/// ```
888+
#[cfg(feature = "std")]
889+
fn reader(self) -> Reader<Self>
890+
where
891+
Self: Sized,
892+
{
893+
reader::new(self)
894+
}
810895
}
811896

812897
macro_rules! deref_forward_buf {

src/buf/buf_mut.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::buf::{limit, Chain, Limit};
2+
#[cfg(feature = "std")]
3+
use crate::buf::{writer, Writer};
4+
15
use core::{
26
cmp,
37
mem::{self, MaybeUninit},
@@ -833,6 +837,83 @@ pub trait BufMut {
833837
fn put_f64_le(&mut self, n: f64) {
834838
self.put_u64_le(n.to_bits());
835839
}
840+
841+
/// Creates an adaptor which can write at most `limit` bytes to `self`.
842+
///
843+
/// # Examples
844+
///
845+
/// ```
846+
/// use bytes::BufMut;
847+
///
848+
/// let arr = &mut [0u8; 128][..];
849+
/// assert_eq!(arr.remaining_mut(), 128);
850+
///
851+
/// let dst = arr.limit(10);
852+
/// assert_eq!(dst.remaining_mut(), 10);
853+
/// ```
854+
fn limit(self, limit: usize) -> Limit<Self>
855+
where
856+
Self: Sized,
857+
{
858+
limit::new(self, limit)
859+
}
860+
861+
/// Creates an adaptor which implements the `Write` trait for `self`.
862+
///
863+
/// This function returns a new value which implements `Write` by adapting
864+
/// the `Write` trait functions to the `BufMut` trait functions. Given that
865+
/// `BufMut` operations are infallible, none of the `Write` functions will
866+
/// return with `Err`.
867+
///
868+
/// # Examples
869+
///
870+
/// ```
871+
/// use bytes::BufMut;
872+
/// use std::io::Write;
873+
///
874+
/// let mut buf = vec![].writer();
875+
///
876+
/// let num = buf.write(&b"hello world"[..]).unwrap();
877+
/// assert_eq!(11, num);
878+
///
879+
/// let buf = buf.into_inner();
880+
///
881+
/// assert_eq!(*buf, b"hello world"[..]);
882+
/// ```
883+
#[cfg(feature = "std")]
884+
fn writer(self) -> Writer<Self>
885+
where
886+
Self: Sized,
887+
{
888+
writer::new(self)
889+
}
890+
891+
/// Creates an adapter which will chain this buffer with another.
892+
///
893+
/// The returned `BufMut` instance will first write to all bytes from
894+
/// `self`. Afterwards, it will write to `next`.
895+
///
896+
/// # Examples
897+
///
898+
/// ```
899+
/// use bytes::BufMut;
900+
///
901+
/// let mut a = [0u8; 5];
902+
/// let mut b = [0u8; 6];
903+
///
904+
/// let mut chain = (&mut a[..]).chain_mut(&mut b[..]);
905+
///
906+
/// chain.put_slice(b"hello world");
907+
///
908+
/// assert_eq!(&a[..], b"hello");
909+
/// assert_eq!(&b[..], b" world");
910+
/// ```
911+
fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
912+
where
913+
Self: Sized,
914+
{
915+
Chain::new(self, next)
916+
}
836917
}
837918

838919
macro_rules! deref_forward_bufmut {

src/buf/ext/chain.rs renamed to src/buf/chain.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::io::IoSlice;
1818
/// # Examples
1919
///
2020
/// ```
21-
/// use bytes::{Bytes, Buf, buf::BufExt};
21+
/// use bytes::{Bytes, Buf};
2222
///
2323
/// let mut buf = (&b"hello "[..])
2424
/// .chain(&b"world"[..]);
@@ -47,7 +47,7 @@ impl<T, U> Chain<T, U> {
4747
/// # Examples
4848
///
4949
/// ```
50-
/// use bytes::buf::BufExt;
50+
/// use bytes::Buf;
5151
///
5252
/// let buf = (&b"hello"[..])
5353
/// .chain(&b"world"[..]);
@@ -63,7 +63,7 @@ impl<T, U> Chain<T, U> {
6363
/// # Examples
6464
///
6565
/// ```
66-
/// use bytes::{Buf, buf::BufExt};
66+
/// use bytes::Buf;
6767
///
6868
/// let mut buf = (&b"hello"[..])
6969
/// .chain(&b"world"[..]);
@@ -82,7 +82,7 @@ impl<T, U> Chain<T, U> {
8282
/// # Examples
8383
///
8484
/// ```
85-
/// use bytes::buf::BufExt;
85+
/// use bytes::Buf;
8686
///
8787
/// let buf = (&b"hello"[..])
8888
/// .chain(&b"world"[..]);
@@ -98,7 +98,7 @@ impl<T, U> Chain<T, U> {
9898
/// # Examples
9999
///
100100
/// ```
101-
/// use bytes::{Buf, buf::BufExt};
101+
/// use bytes::Buf;
102102
///
103103
/// let mut buf = (&b"hello "[..])
104104
/// .chain(&b"world"[..]);
@@ -117,7 +117,7 @@ impl<T, U> Chain<T, U> {
117117
/// # Examples
118118
///
119119
/// ```
120-
/// use bytes::buf::BufExt;
120+
/// use bytes::Buf;
121121
///
122122
/// let chain = (&b"hello"[..])
123123
/// .chain(&b"world"[..]);

0 commit comments

Comments
 (0)