Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xdr-codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ unstable = []

[dependencies]
byteorder = "1.0"
error-chain = "0.10"
error-chain = "0.12"

[dev-dependencies]
quickcheck = "0.4"
134 changes: 70 additions & 64 deletions xdr-codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ extern crate byteorder;
#[macro_use]
extern crate error_chain;

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::borrow::{Borrow, Cow};
use std::cmp::min;
pub use std::io::{Read, Write};
use std::ops::Deref;
use std::cmp::min;
use std::borrow::{Borrow, Cow};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

pub mod record;

Expand Down Expand Up @@ -193,9 +193,11 @@ where
T: Unpack<In> + Clone,
{
#[inline]
fn set<T>(p: &mut T, v: T) { *p = v }
fn set<T>(p: &mut T, v: T) {
*p = v
}
#[inline]
fn drop<T>(_: &mut T) { }
fn drop<T>(_: &mut T) {}

unpack_array_with(input, array, arraysz, set, drop, defl)
}
Expand All @@ -207,7 +209,7 @@ pub fn unpack_array_with<In, T>(
input: &mut In,
array: &mut [T],
arraysz: usize,
set: fn (&mut T, T),
set: fn(&mut T, T),
drop: fn(&mut T),
defl: Option<&T>,
) -> Result<usize>
Expand All @@ -217,24 +219,24 @@ where
{
let mut rsz = 0;
let sz = min(arraysz, array.len());

// If we fail part way through then return the error and the index we got up to
// so we can clean up the entries we did initialize.
let res = (|| {
for (idx, elem) in (&mut array[..sz]).into_iter().enumerate() {
let (v, sz) = match Unpack::unpack(input) {
Ok(v) => v,
Err(e) => return Some((idx, e)),
};
rsz += sz;
set(elem, v);
}
None
})();
for (idx, elem) in (&mut array[..sz]).into_iter().enumerate() {
let (v, sz) = match Unpack::unpack(input) {
Ok(v) => v,
Err(e) => return Some((idx, e)),
};
rsz += sz;
set(elem, v);
}
None
})();
if let Some((idx, err)) = res {
for elem in &mut array[..idx] {
drop(elem)
};
}
return Err(err);
}

Expand Down Expand Up @@ -398,54 +400,54 @@ impl<Out: Write> Pack<Out> for i8 {
impl<Out: Write> Pack<Out> for u32 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_u32::<BigEndian>(*self).map_err(Error::from).map(
|_| 4,
)
out.write_u32::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 4)
}
}

impl<Out: Write> Pack<Out> for i32 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_i32::<BigEndian>(*self).map_err(Error::from).map(
|_| 4,
)
out.write_i32::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 4)
}
}

impl<Out: Write> Pack<Out> for u64 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_u64::<BigEndian>(*self).map_err(Error::from).map(
|_| 8,
)
out.write_u64::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 8)
}
}

impl<Out: Write> Pack<Out> for i64 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_i64::<BigEndian>(*self).map_err(Error::from).map(
|_| 8,
)
out.write_i64::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 8)
}
}

impl<Out: Write> Pack<Out> for f32 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_f32::<BigEndian>(*self).map_err(Error::from).map(
|_| 4,
)
out.write_f32::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 4)
}
}

impl<Out: Write> Pack<Out> for f64 {
#[inline]
fn pack(&self, out: &mut Out) -> Result<usize> {
out.write_f64::<BigEndian>(*self).map_err(Error::from).map(
|_| 8,
)
out.write_f64::<BigEndian>(*self)
.map_err(Error::from)
.map(|_| 8)
}
}

Expand Down Expand Up @@ -587,75 +589,79 @@ pub trait Unpack<In: Read>: Sized {
impl<In: Read> Unpack<In> for u8 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_u32::<BigEndian>().map_err(Error::from).map(
|v| {
(v as u8, 4)
},
)
input
.read_u32::<BigEndian>()
.map_err(Error::from)
.map(|v| (v as u8, 4))
}
}

#[cfg(feature = "bytecodec")]
impl<In: Read> Unpack<In> for i8 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_i32::<BigEndian>().map_err(Error::from).map(
|v| {
(v as i8, 4)
},
)
input
.read_i32::<BigEndian>()
.map_err(Error::from)
.map(|v| (v as i8, 4))
}
}

impl<In: Read> Unpack<In> for u32 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_u32::<BigEndian>().map_err(Error::from).map(
|v| (v, 4),
)
input
.read_u32::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 4))
}
}

impl<In: Read> Unpack<In> for i32 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_i32::<BigEndian>().map_err(Error::from).map(
|v| (v, 4),
)
input
.read_i32::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 4))
}
}

impl<In: Read> Unpack<In> for u64 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_u64::<BigEndian>().map_err(Error::from).map(
|v| (v, 8),
)
input
.read_u64::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 8))
}
}

impl<In: Read> Unpack<In> for i64 {
#[inline]
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_i64::<BigEndian>().map_err(Error::from).map(
|v| (v, 8),
)
input
.read_i64::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 8))
}
}

impl<In: Read> Unpack<In> for f32 {
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_f32::<BigEndian>().map_err(Error::from).map(
|v| (v, 4),
)
input
.read_f32::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 4))
}
}

impl<In: Read> Unpack<In> for f64 {
fn unpack(input: &mut In) -> Result<(Self, usize)> {
input.read_f64::<BigEndian>().map_err(Error::from).map(
|v| (v, 8),
)
input
.read_f64::<BigEndian>()
.map_err(Error::from)
.map(|v| (v, 8))
}
}

Expand Down
29 changes: 16 additions & 13 deletions xdr-codec/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
//!
//! There's no magic number or other way to determine whether a stream
//! is using record marking; both ends must agree.
use std::io::{self, BufRead, Read, Write};
use std::cmp::min;
use std::io::{self, BufRead, Read, Write};

use error::*;

use super::{Error, pack, unpack};
use super::{pack, unpack, Error};

const LAST_REC: u32 = 1u32 << 31;

Expand All @@ -36,9 +36,9 @@ fn mapioerr(xdrerr: Error) -> io::Error {
/// determine record ends.
#[derive(Debug)]
pub struct XdrRecordReader<R: BufRead> {
size: usize, // record size
size: usize, // record size
consumed: usize, // bytes consumed
eor: bool, // is last record
eor: bool, // is last record

reader: R, // reader
}
Expand All @@ -62,7 +62,10 @@ impl<R: BufRead> XdrRecordReader<R> {
let rechdr: u32 = match unpack(&mut self.reader) {
Ok(v) => v,
Err(Error(ErrorKind::IOError(ref err), _))
if err.kind() == io::ErrorKind::UnexpectedEof => return Ok(true),
if err.kind() == io::ErrorKind::UnexpectedEof =>
{
return Ok(true)
}
Err(e) => return Err(mapioerr(e)),
};

Expand Down Expand Up @@ -151,19 +154,19 @@ impl<R: BufRead> Iterator for XdrRecordReaderIter<R> {
// Do we need next fragment?
if rr.totremains() == 0 {
match rr.nextrec() {
Err(e) => return Some(Err(e)), // IO error
Ok(true) => return None, // EOF
Ok(false) => (), // keep going
Err(e) => return Some(Err(e)), // IO error
Ok(true) => return None, // EOF
Ok(false) => (), // keep going
}
}

let remains = rr.totremains();
let eor = rr.eor();

match rr.by_ref().take(remains as u64).read_to_end(&mut buf) {
Ok(sz) if sz == remains => (), // OK, keep going
Ok(_) => return None, // short read
Err(e) => return Some(Err(e)), // error
Ok(sz) if sz == remains => (), // OK, keep going
Ok(_) => return None, // short read
Err(e) => return Some(Err(e)), // error
};

if eor {
Expand All @@ -186,8 +189,8 @@ const WRBUF: usize = 65536;
pub struct XdrRecordWriter<W: Write> {
buf: Vec<u8>, // accumulated record fragment
bufsz: usize, // max fragment size
eor: bool, // last fragment was eor
writer: W, // writer we're passing on to
eor: bool, // last fragment was eor
writer: W, // writer we're passing on to
}

impl<W: Write> XdrRecordWriter<W> {
Expand Down
4 changes: 2 additions & 2 deletions xdr-codec/tests/qc-record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ extern crate xdr_codec;

use std::io::{Cursor, Write};

use quickcheck::{TestResult, quickcheck};
use quickcheck::{quickcheck, TestResult};

use xdr_codec::Pack;
use xdr_codec::record::{XdrRecordReader, XdrRecordWriter};
use xdr_codec::Pack;

// Make sure XdrRecordWriter writes the right stuff
fn check_writerec(bufsz: usize, eor: bool, ref bytes: Vec<u8>) -> TestResult {
Expand Down
Loading