mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-13 00:00:32 +02:00
Add IntoBuf trait
This commit is contained in:
@@ -363,6 +363,40 @@ pub trait MutBuf {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* ===== IntoBuf =====
|
||||
*
|
||||
*/
|
||||
|
||||
/// Conversion into a `Buf`
|
||||
///
|
||||
/// Usually, `IntoBuf` is implemented on references of types and not directly
|
||||
/// on the types themselves. For example, `IntoBuf` is implemented for `&'a
|
||||
/// Vec<u8>` and not `Vec<u8>` directly.
|
||||
pub trait IntoBuf {
|
||||
type Buf: Buf;
|
||||
|
||||
fn into_buf(self) -> Self::Buf;
|
||||
}
|
||||
|
||||
impl<'a> IntoBuf for &'a [u8] {
|
||||
type Buf = io::Cursor<&'a [u8]>;
|
||||
|
||||
/// Creates a buffer from a value
|
||||
fn into_buf(self) -> Self::Buf {
|
||||
io::Cursor::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoBuf for &'a Vec<u8> {
|
||||
type Buf = io::Cursor<&'a [u8]>;
|
||||
|
||||
fn into_buf(self) -> Self::Buf {
|
||||
io::Cursor::new(&self[..])
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* ===== Sink / Source =====
|
||||
|
||||
Reference in New Issue
Block a user