From 974043bd134c69d1b0456f63c212bd48419d29eb Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 10 Feb 2015 19:29:39 -0800 Subject: [PATCH] Add read_byte and write_byte --- src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5a4eb88..fddd320 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,7 @@ pub trait Buf { self.remaining() > 0 } - /// Read bytes from this Buf into the given slice and advance the cursor by + /// Read bytes from the `Buf` into the given slice and advance the cursor by /// the number of bytes read. /// /// If there are fewer bytes remaining than is needed to satisfy the @@ -86,6 +86,17 @@ pub trait Buf { len } + + /// Read a single byte from the `Buf` + fn read_byte(&mut self) -> Option { + let mut dst = [0]; + + if self.read_slice(&mut dst) == 0 { + return None; + } + + Some(dst[0]) + } } pub trait BufExt { @@ -156,6 +167,17 @@ pub trait MutBuf : Sized { len } + + /// Write a single byte to the `MuBuf` + fn write_byte(&mut self, byte: u8) -> bool { + let src = [byte]; + + if self.write_slice(&src) == 0 { + return false; + } + + true + } } pub trait MutBufExt {