From e4fd3924589550725a0cd297ed191e04d9a66e2f Mon Sep 17 00:00:00 2001 From: Andrew Hobden <andrew@hoverbear.org> Date: Mon, 30 Mar 2015 14:42:59 -0700 Subject: [PATCH] Add Read and Write to RingBuf --- src/ring.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/ring.rs b/src/ring.rs index fe29db4..4f9fdb9 100644 --- a/src/ring.rs +++ b/src/ring.rs @@ -1,6 +1,7 @@ use super::{Buf, MutBuf}; use std::{cmp, fmt, mem, ptr, slice}; use std::rt::heap; +use std::io; /// 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, @@ -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 { } -- GitLab