mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-20 00:00:10 +02:00
Add Read and Write to RingBuf
This commit is contained in:
+25
@@ -1,6 +1,7 @@
|
|||||||
use super::{Buf, MutBuf};
|
use super::{Buf, MutBuf};
|
||||||
use std::{cmp, fmt, mem, ptr, slice};
|
use std::{cmp, fmt, mem, ptr, slice};
|
||||||
use std::rt::heap;
|
use std::rt::heap;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
/// Buf backed by a continous chunk of memory. Maintains a read cursor and a
|
/// Buf backed by a continous chunk of memory. Maintains a read cursor and a
|
||||||
/// write cursor. When reads and writes reach the end of the allocated buffer,
|
/// write cursor. When reads and writes reach the end of the allocated buffer,
|
||||||
@@ -184,4 +185,28 @@ impl MutBuf for RingBuf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl io::Read for RingBuf {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
if !MutBuf::has_remaining(self) {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(self.read_slice(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl io::Write for RingBuf {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
if !Buf::has_remaining(self) {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(self.write_slice(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe impl Send for RingBuf { }
|
unsafe impl Send for RingBuf { }
|
||||||
|
|||||||
Reference in New Issue
Block a user