From 92d6b95c2c52d6b96cc8353606e3eb6f1c35b2df Mon Sep 17 00:00:00 2001 From: Ty Overby Date: Sun, 12 Apr 2015 21:04:15 -0700 Subject: [PATCH] remove reimplementation of read_exactly --- src/lib.rs | 2 +- src/reader.rs | 91 +++++---------------------------------------------- src/test.rs | 5 +++ 3 files changed, 14 insertions(+), 84 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8f1edfe..3f6c697 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ #![crate_type = "dylib"] #![doc(html_logo_url = "./icon.png")] -#![feature(core, unicode)] +#![feature(unicode)] extern crate rustc_serialize; extern crate byteorder; diff --git a/src/reader.rs b/src/reader.rs index 5c5d127..67be642 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,7 +1,5 @@ use std::io::Read; use std::io::Error as IoError; -use std::io::Result as IoResult; -use std::num::{cast, NumCast}; use std::error::Error; use std::fmt; use std::convert::From; @@ -122,9 +120,7 @@ impl<'a, R: Read> DecoderReader<'a, R> { } impl <'a, A> DecoderReader<'a, A> { - fn read_bytes(&mut self, count: I) -> Result<(), DecodingError> - where I: NumCast { - let count: u64 = cast(count).unwrap(); + fn read_bytes(&mut self, count: u64) -> Result<(), DecodingError> { self.read += count; match self.size_limit { SizeLimit::Infinite => Ok(()), @@ -135,7 +131,7 @@ impl <'a, A> DecoderReader<'a, A> { fn read_type(&mut self) -> Result<(), DecodingError> { use std::mem::size_of; - self.read_bytes(size_of::()) + self.read_bytes(size_of::() as u64) } } @@ -217,6 +213,7 @@ impl<'a, R: Read> Decoder for DecoderReader<'a, R> { let width = unicode::str::utf8_char_width(first_byte); if width == 1 { return Ok(first_byte as char) } if width == 0 { return Err(error)} + let mut buf = [first_byte, 0, 0, 0]; { let mut start = 1; @@ -234,16 +231,17 @@ impl<'a, R: Read> Decoder for DecoderReader<'a, R> { None => Err(error) }); - try!(self.read_bytes(res.len_utf8())); + try!(self.read_bytes(res.len_utf8() as u64)); Ok(res) } fn read_str(&mut self) -> DecodingResult { let len = try!(self.read_usize()); - try!(self.read_bytes(len)); + try!(self.read_bytes(len as u64)); - let vector = try!(read_exact(&mut self.reader, len)); - match String::from_utf8(vector) { + let mut buff = Vec::new(); + try!(self.reader.by_ref().take(len as u64).read_to_end(&mut buff)); + match String::from_utf8(buff) { Ok(s) => Ok(s), Err(err) => Err(DecodingError::InvalidEncoding(InvalidEncoding { desc: "error while decoding utf8 string", @@ -353,76 +351,3 @@ impl<'a, R: Read> Decoder for DecoderReader<'a, R> { }) } } - -fn read_at_least(reader: &mut R, min: usize, buf: &mut [u8]) -> IoResult { - use std::io::ErrorKind; - if min > buf.len() { - return Err(IoError::new( - ErrorKind::InvalidInput, "the buffer is too short")); - } - - let mut read = 0; - while read < min { - let mut zeroes = 0; - loop { - match reader.read(&mut buf[read..]) { - Ok(0) => { - zeroes += 1; - if zeroes >= 1000 { - return Err(IoError::new(ErrorKind::Other, - "no progress was made")); - } - } - Ok(n) => { - read += n; - break; - } - err@Err(_) => return err - } - } - } - Ok(read) -} - -unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec, start: usize, end: usize) -> &'a mut [T] { - use std::raw::Slice; - //use std::ptr::PtrExt; - use std::mem::transmute; - - assert!(start <= end); - assert!(end <= v.capacity()); - transmute(Slice { - data: v.as_ptr().offset(start as isize), - len: end - start - }) -} - - -fn push_at_least(reader: &mut R, min: usize, len: usize, buf: &mut Vec) -> IoResult { - use std::io::ErrorKind; - if min > len { - return Err(IoError::new(ErrorKind::InvalidInput, "the buffer is too short")); - } - - let start_len = buf.len(); - buf.reserve(len); - - - let mut read = 0; - while read < min { - read += { - let s = unsafe { slice_vec_capacity(buf, start_len + read, start_len + len) }; - try!(read_at_least(reader, 1, s)) - }; - unsafe { buf.set_len(start_len + read) }; - } - Ok(read) -} - -fn read_exact(reader: &mut R, len: usize) -> IoResult> { - let mut buf = Vec::with_capacity(len); - match push_at_least(reader, len, len, &mut buf) { - Ok(_) => Ok(buf), - Err(e) => Err(e), - } -} diff --git a/src/test.rs b/src/test.rs index f30257e..b55c1cf 100644 --- a/src/test.rs +++ b/src/test.rs @@ -297,3 +297,8 @@ fn test_refbox() { } } } + +#[test] +fn test_multi_strings() { + assert!(encode(&("foo", "bar", "baz"), Infinite).is_ok()); +}