From e30ade6ed302049d255bb223d66222bf68fd9e67 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 5 Jul 2021 13:00:30 +0200 Subject: [PATCH] Take more advantage of use std::io; and fix benches --- .../rust/preserves-schema/src/support/mod.rs | 10 +-- .../rust/preserves/benches/codec.rs | 80 ++++++++++--------- .../rust/preserves/examples/extensibility.rs | 6 +- implementations/rust/preserves/src/error.rs | 29 +++---- .../rust/preserves/src/value/domain.rs | 4 +- .../preserves/src/value/packed/constants.rs | 6 +- .../rust/preserves/src/value/packed/writer.rs | 14 ++-- .../rust/preserves/src/value/reader.rs | 7 +- .../rust/preserves/src/value/writer.rs | 2 +- .../rust/preserves/tests/samples_tests.rs | 5 +- 10 files changed, 86 insertions(+), 77 deletions(-) diff --git a/implementations/rust/preserves-schema/src/support/mod.rs b/implementations/rust/preserves-schema/src/support/mod.rs index f4049ce..bddfd3e 100644 --- a/implementations/rust/preserves-schema/src/support/mod.rs +++ b/implementations/rust/preserves-schema/src/support/mod.rs @@ -39,7 +39,7 @@ where #[derive(Debug)] pub enum ParseError { ConformanceError, - IO(std::io::Error), + IO(io::Error), Preserves(preserves::error::Error), } @@ -57,8 +57,8 @@ impl std::fmt::Display for ParseError { impl std::error::Error for ParseError {} -impl From for ParseError { - fn from(v: std::io::Error) -> Self { +impl From for ParseError { + fn from(v: io::Error) -> Self { ParseError::IO(v) } } @@ -69,11 +69,11 @@ impl From for ParseError { } } -impl From for std::io::Error { +impl From for io::Error { fn from(v: ParseError) -> Self { match v { ParseError::ConformanceError => - std::io::Error::new(std::io::ErrorKind::InvalidData, INPUT_NOT_CONFORMANT), + io::Error::new(io::ErrorKind::InvalidData, INPUT_NOT_CONFORMANT), ParseError::IO(e) => e, ParseError::Preserves(e) => e.into(), } diff --git a/implementations/rust/preserves/benches/codec.rs b/implementations/rust/preserves/benches/codec.rs index fad2ac1..521b954 100644 --- a/implementations/rust/preserves/benches/codec.rs +++ b/implementations/rust/preserves/benches/codec.rs @@ -1,50 +1,57 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use preserves::value::{self, Reader, Writer, PackedReader, PackedWriter}; -use preserves::{de, ser}; +use preserves::de; +use preserves::ser; +use preserves::value::BinarySource; +use preserves::value::BytesBinarySource; +use preserves::value::IOBinarySource; +use preserves::value::IOValueDomainCodec; +use preserves::value::PackedWriter; +use preserves::value::Reader; +use preserves::value::Writer; +use preserves::value::packed::annotated_iovalue_from_bytes; +use preserves::value; +use std::fs::File; use std::io::Read; -use std::io::BufReader; -use std::io::{Seek, SeekFrom}; +use std::io::Seek; +use std::io; #[path = "../tests/samples/mod.rs"] mod samples; use samples::TestCases; pub fn bench_decoder_bytes(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); c.bench_function("decode samples.bin via bytes", |b| b.iter_with_large_drop( - || PackedReader::decode_bytes(&bs[..]).demand_next(true).unwrap())); + || annotated_iovalue_from_bytes(&bs[..]).unwrap())); } pub fn bench_decoder_file(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); c.bench_function("decode samples.bin via file", |b| b.iter_with_large_drop(|| { - fh.seek(SeekFrom::Start(0)).ok(); - PackedReader::decode_read(&mut fh).demand_next(true).unwrap() + fh.seek(io::SeekFrom::Start(0)).ok(); + IOBinarySource::new(&mut fh).packed_iovalues().demand_next(true).unwrap() })); } pub fn bench_decoder_buffered_file(c: &mut Criterion) { - let mut fh = BufReader::new(std::fs::File::open("../../../tests/samples.bin").unwrap()); + let mut fh = io::BufReader::new(File::open("../../../tests/samples.bin").unwrap()); c.bench_function("decode samples.bin via buffered file", |b| b.iter_with_large_drop(|| { - fh.seek(SeekFrom::Start(0)).ok(); - PackedReader::decode_read(&mut fh).demand_next(true).unwrap() + fh.seek(io::SeekFrom::Start(0)).ok(); + IOBinarySource::new(&mut fh).packed_iovalues().demand_next(true).unwrap() })); } pub fn bench_encoder(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); - let v = PackedReader::decode_read(&mut fh).demand_next(true).unwrap(); - c.bench_function("encode samples.bin", |b| b.iter_with_large_drop(|| { - let mut bs = vec![]; - PackedWriter::new(&mut bs).write(&v).unwrap(); - bs - })); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); + let v = IOBinarySource::new(&mut fh).packed_iovalues().demand_next(true).unwrap(); + c.bench_function("encode samples.bin", |b| b.iter_with_large_drop( + || PackedWriter::encode_iovalue(&v).unwrap())); } pub fn bench_de(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); c.bench_function("deserialize samples.bin", |b| b.iter_with_large_drop( @@ -52,7 +59,7 @@ pub fn bench_de(c: &mut Criterion) { } pub fn bench_ser(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); let v: TestCases = de::from_read(&mut fh).unwrap(); c.bench_function("serialize samples.bin", |b| b.iter_with_large_drop(|| { let mut bs = vec![]; @@ -62,30 +69,28 @@ pub fn bench_ser(c: &mut Criterion) { } pub fn bench_decoder_de(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); c.bench_function("decode-then-deserialize samples.bin", |b| b.iter_with_large_drop( - || value::de::from_value::(&PackedReader::decode_bytes(&bs[..]).demand_next(true).unwrap()).unwrap())); + || value::de::from_value::(&annotated_iovalue_from_bytes(&bs[..]).unwrap()).unwrap())); } pub fn bench_ser_encoder(c: &mut Criterion) { - let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); + let mut fh = File::open("../../../tests/samples.bin").unwrap(); let v: TestCases = de::from_read(&mut fh).unwrap(); - c.bench_function("serialize-then-encode samples.bin", |b| b.iter_with_large_drop(|| { - let mut bs = vec![]; - PackedWriter::new(&mut bs).write(&value::ser::to_value(&v)).unwrap(); - bs - })); + c.bench_function("serialize-then-encode samples.bin", |b| b.iter_with_large_drop( + || PackedWriter::encode_iovalue(&value::ser::to_value(&v)).unwrap())); } pub fn large_testdata_decoder_with_ann(c: &mut Criterion) { c.bench_function("decode testdata.bin with annotations", |b| { - let mut fh = std::fs::File::open("benches/testdata.bin").unwrap(); + let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); b.iter(|| { - let mut r = PackedReader::decode_bytes(&bs[..]); + let mut src = BytesBinarySource::new(&bs[..]); + let mut r = src.packed_iovalues(); while let Some(_) = r.next(true).unwrap() {} }) }); @@ -93,11 +98,12 @@ pub fn large_testdata_decoder_with_ann(c: &mut Criterion) { pub fn large_testdata_decoder_without_ann(c: &mut Criterion) { c.bench_function("decode testdata.bin without annotations", |b| { - let mut fh = std::fs::File::open("benches/testdata.bin").unwrap(); + let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); b.iter(|| { - let mut r = PackedReader::decode_bytes(&bs[..]); + let mut src = BytesBinarySource::new(&bs[..]); + let mut r = src.packed_iovalues(); while let Some(_) = r.next(false).unwrap() {} }) }); @@ -105,17 +111,19 @@ pub fn large_testdata_decoder_without_ann(c: &mut Criterion) { pub fn large_testdata_encoder(c: &mut Criterion) { c.bench_function("encode testdata.bin", |b| { - let mut fh = BufReader::new(std::fs::File::open("benches/testdata.bin").unwrap()); + let mut fh = io::BufReader::new(File::open("benches/testdata.bin").unwrap()); let mut vs = vec![]; - let mut r = PackedReader::decode_read(&mut fh); + let mut src = IOBinarySource::new(&mut fh); + let mut r = src.packed_iovalues(); while let Some(v) = r.next(true).unwrap() { vs.push(v); } b.iter_with_large_drop(|| { let mut bs = vec![]; let mut w = PackedWriter::new(&mut bs); + let mut enc = IOValueDomainCodec; for v in &vs { - w.write(&v).unwrap(); + w.write(&mut enc, v).unwrap(); } bs }) diff --git a/implementations/rust/preserves/examples/extensibility.rs b/implementations/rust/preserves/examples/extensibility.rs index 31b96e2..a0a0e41 100644 --- a/implementations/rust/preserves/examples/extensibility.rs +++ b/implementations/rust/preserves/examples/extensibility.rs @@ -1,7 +1,7 @@ use preserves::{de, value::{self, Reader, IOBinarySource, BinarySource}}; use serde::{Serialize, Deserialize}; use std::fs::File; -use std::io::Error; +use std::io; #[derive(Debug, Serialize, Deserialize, PartialEq)] enum Fruit { @@ -32,7 +32,7 @@ enum Variety { Anjou, } -fn try_file(kind: &str, path: &str) -> Result<(), Error> { +fn try_file(kind: &str, path: &str) -> io::Result<()> { let fruits_value = IOBinarySource::new(&mut File::open(path)?).packed_iovalues().demand_next(true)?; println!("{:?}", fruits_value); @@ -46,7 +46,7 @@ fn try_file(kind: &str, path: &str) -> Result<(), Error> { Ok(()) } -fn main() -> Result<(), Error> { +fn main() -> io::Result<()> { try_file("KNOWN", "examples/known-data.bin")?; try_file("UNKNOWN", "examples/unknown-data.bin")?; Ok(()) diff --git a/implementations/rust/preserves/src/error.rs b/implementations/rust/preserves/src/error.rs index d86513f..4e92ac6 100644 --- a/implementations/rust/preserves/src/error.rs +++ b/implementations/rust/preserves/src/error.rs @@ -1,9 +1,10 @@ use num::bigint::BigInt; use std::convert::From; +use std::io; #[derive(Debug)] pub enum Error { - Io(std::io::Error), + Io(io::Error), Message(String), InvalidUnicodeScalar(u32), NumberOutOfRange(BigInt), @@ -48,18 +49,18 @@ pub enum ExpectedKind { UnicodeScalar, } -impl From for Error { - fn from(e: std::io::Error) -> Self { +impl From for Error { + fn from(e: io::Error) -> Self { Error::Io(e) } } -impl From for std::io::Error { +impl From for io::Error { fn from(e: Error) -> Self { match e { Error::Io(ioe) => ioe, - Error::Message(str) => std::io::Error::new(std::io::ErrorKind::Other, str), - _ => std::io::Error::new(std::io::ErrorKind::Other, e.to_string()), + Error::Message(str) => io::Error::new(io::ErrorKind::Other, str), + _ => io::Error::new(io::ErrorKind::Other, e.to_string()), } } } @@ -116,18 +117,18 @@ pub fn is_syntax_error(e: &Error) -> bool { //--------------------------------------------------------------------------- -pub fn io_eof() -> std::io::Error { - std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "EOF") +pub fn io_eof() -> io::Error { + io::Error::new(io::ErrorKind::UnexpectedEof, "EOF") } -pub fn is_eof_io_error(e: &std::io::Error) -> bool { - matches!(e.kind(), std::io::ErrorKind::UnexpectedEof) +pub fn is_eof_io_error(e: &io::Error) -> bool { + matches!(e.kind(), io::ErrorKind::UnexpectedEof) } -pub fn io_syntax_error(s: &str) -> std::io::Error { - std::io::Error::new(std::io::ErrorKind::InvalidData, s) +pub fn io_syntax_error(s: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, s) } -pub fn is_syntax_io_error(e: &std::io::Error) -> bool { - matches!(e.kind(), std::io::ErrorKind::InvalidData) +pub fn is_syntax_io_error(e: &io::Error) -> bool { + matches!(e.kind(), io::ErrorKind::InvalidData) } diff --git a/implementations/rust/preserves/src/value/domain.rs b/implementations/rust/preserves/src/value/domain.rs index 8af8dec..f0139a1 100644 --- a/implementations/rust/preserves/src/value/domain.rs +++ b/implementations/rust/preserves/src/value/domain.rs @@ -53,7 +53,7 @@ impl DomainDecode for NoEmbeddedDomainCodec { _src: &'src mut S, _read_annotations: bool, ) -> io::Result { - Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "Embedded values not supported here")) + Err(io::Error::new(io::ErrorKind::Unsupported, "Embedded values not supported here")) } } @@ -63,6 +63,6 @@ impl DomainEncode for NoEmbeddedDomainCodec { _w: &mut W, _d: &D, ) -> io::Result<()> { - Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "Embedded values not supported here")) + Err(io::Error::new(io::ErrorKind::Unsupported, "Embedded values not supported here")) } } diff --git a/implementations/rust/preserves/src/value/packed/constants.rs b/implementations/rust/preserves/src/value/packed/constants.rs index 22c9313..90feaed 100644 --- a/implementations/rust/preserves/src/value/packed/constants.rs +++ b/implementations/rust/preserves/src/value/packed/constants.rs @@ -1,4 +1,5 @@ use std::convert::{TryFrom, From}; +use std::io; #[derive(Debug, PartialEq, Eq)] pub enum Tag { @@ -24,10 +25,9 @@ pub enum Tag { #[derive(Debug, PartialEq, Eq)] pub struct InvalidTag(u8); -impl From for std::io::Error { +impl From for io::Error { fn from(v: InvalidTag) -> Self { - std::io::Error::new(std::io::ErrorKind::InvalidData, - format!("Invalid Preserves tag {}", v.0)) + io::Error::new(io::ErrorKind::InvalidData, format!("Invalid Preserves tag {}", v.0)) } } diff --git a/implementations/rust/preserves/src/value/packed/writer.rs b/implementations/rust/preserves/src/value/packed/writer.rs index 978565c..2b73347 100644 --- a/implementations/rust/preserves/src/value/packed/writer.rs +++ b/implementations/rust/preserves/src/value/packed/writer.rs @@ -72,7 +72,7 @@ impl DerefMut for Suspendable { } } -pub struct PackedWriter(Suspendable); +pub struct PackedWriter(Suspendable); impl PackedWriter> { pub fn encode, Enc: DomainEncode>( @@ -85,12 +85,12 @@ impl PackedWriter> { Ok(buf) } - pub fn encode_iovalue(v: &IOValue) -> std::io::Result> { + pub fn encode_iovalue(v: &IOValue) -> io::Result> { Self::encode(&mut IOValueDomainCodec, v) } } -impl PackedWriter { +impl PackedWriter { pub fn new(write: W) -> Self { PackedWriter(Suspendable::new(write)) } @@ -171,7 +171,7 @@ pub trait WriteWriter: Writer { } } -impl WriteWriter for PackedWriter { +impl WriteWriter for PackedWriter { fn write_raw_bytes(&mut self, v: &[u8]) -> io::Result<()> { self.w().write_all(v) } @@ -179,7 +179,7 @@ impl WriteWriter for PackedWriter { impl WriteWriter for BinaryOrderWriter { fn write_raw_bytes(&mut self, v: &[u8]) -> io::Result<()> { - use std::io::Write; + use io::Write; self.buffer().write_all(v) } } @@ -194,7 +194,7 @@ impl AnnotationWriter for T { } } -impl CompoundWriter for PackedWriter { +impl CompoundWriter for PackedWriter { fn extend(&mut self) -> io::Result<()> { Ok(()) } @@ -301,7 +301,7 @@ macro_rules! fits_in_bytes { }) } -impl Writer for PackedWriter +impl Writer for PackedWriter { type AnnWriter = Self; type SeqWriter = Self; diff --git a/implementations/rust/preserves/src/value/reader.rs b/implementations/rust/preserves/src/value/reader.rs index a15742e..3c05f24 100644 --- a/implementations/rust/preserves/src/value/reader.rs +++ b/implementations/rust/preserves/src/value/reader.rs @@ -2,7 +2,6 @@ use crate::error::{self, ExpectedKind, Received, io_eof}; use std::borrow::Cow; use std::io; -use std::io::Read; use std::marker::PhantomData; use super::{DomainDecode, Embeddable, IOValue, IOValueDomainCodec, NestedValue}; @@ -180,18 +179,18 @@ pub trait BinarySource<'de>: Sized { } } -pub struct IOBinarySource<'a, R: Read> { +pub struct IOBinarySource<'a, R: io::Read> { pub read: &'a mut R, pub buf: Option, } -impl<'a, R: Read> IOBinarySource<'a, R> { +impl<'a, R: io::Read> IOBinarySource<'a, R> { pub fn new(read: &'a mut R) -> Self { IOBinarySource { read, buf: None } } } -impl<'de, 'a, R: Read> BinarySource<'de> for IOBinarySource<'a, R> { +impl<'de, 'a, R: io::Read> BinarySource<'de> for IOBinarySource<'a, R> { fn skip(&mut self) -> io::Result<()> { if self.buf.is_none() { unreachable!(); } self.buf = None; diff --git a/implementations/rust/preserves/src/value/writer.rs b/implementations/rust/preserves/src/value/writer.rs index df35de1..f6e8dee 100644 --- a/implementations/rust/preserves/src/value/writer.rs +++ b/implementations/rust/preserves/src/value/writer.rs @@ -148,7 +148,7 @@ pub trait Writer: Sized { } } -pub fn varint(w: &mut W, mut v: u64) -> io::Result { +pub fn varint(w: &mut W, mut v: u64) -> io::Result { let mut byte_count = 0; loop { byte_count += 1; diff --git a/implementations/rust/preserves/tests/samples_tests.rs b/implementations/rust/preserves/tests/samples_tests.rs index bf8e716..78fa6b1 100644 --- a/implementations/rust/preserves/tests/samples_tests.rs +++ b/implementations/rust/preserves/tests/samples_tests.rs @@ -8,15 +8,16 @@ use preserves::value::PackedWriter; use preserves::value::Reader; use preserves::value::de::from_value as deserialize_from_value; use std::iter::Iterator; +use std::io; mod samples; use samples::*; -fn decode_all(bytes: &'_ [u8]) -> Result, std::io::Error> { +fn decode_all(bytes: &'_ [u8]) -> io::Result> { BytesBinarySource::new(bytes).packed_iovalues().configured(true).collect() } -#[test] fn run() -> std::io::Result<()> { +#[test] fn run() -> io::Result<()> { let mut fh = std::fs::File::open("../../../tests/samples.bin").unwrap(); let mut src = IOBinarySource::new(&mut fh); let mut d = src.packed_iovalues().configured(true);