diff --git a/src/bytestr.rs b/src/bytestr.rs index c9ef284..73fc590 100644 --- a/src/bytestr.rs +++ b/src/bytestr.rs @@ -2,6 +2,20 @@ use std::fmt; use std::hash; use std::ops; +/// A trait that encapsulates a `Vec` or a `&[T]`. +pub trait IntoVector { + /// Convert the underlying value to a vector. + fn into_vec(self) -> Vec; +} + +impl IntoVector for Vec { + fn into_vec(self) -> Vec { self } +} + +impl<'a, T: Clone> IntoVector for &'a [T] { + fn into_vec(self) -> Vec { self.to_vec() } +} + /// A type that represents unadulterated byte strings. /// /// Byte strings represent *any* 8 bit character encoding. There are no @@ -27,7 +41,7 @@ pub struct ByteString(Vec); impl ByteString { /// Create a new byte string from a vector or slice of bytes. - pub fn from_bytes>(bs: S) -> ByteString { + pub fn from_bytes>(bs: S) -> ByteString { ByteString(bs.into_vec()) } diff --git a/src/encoder.rs b/src/encoder.rs index e1f4b76..c071703 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -2,7 +2,7 @@ use std::fmt; use serialize; -use {ByteString, CsvResult, Error, ErrEncode}; +use {ByteString, CsvResult, Error, ErrEncode, IntoVector}; /// A record to be encoded. /// @@ -24,7 +24,7 @@ impl Encoded { /// to access the raw CSV record. pub fn unwrap(self) -> Vec { self.record } - fn push_bytes>(&mut self, s: S) -> CsvResult<()> { + fn push_bytes>(&mut self, s: S) -> CsvResult<()> { self.record.push(ByteString::from_bytes(s)); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index c8b1200..f79f323 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,7 +193,7 @@ extern crate "test" as stdtest; use std::fmt; use std::io; -pub use bytestr::ByteString; +pub use bytestr::{ByteString, IntoVector}; pub use encoder::Encoded; pub use decoder::Decoded; pub use reader::{Reader, DecodedRecords, StringRecords, ByteRecords}; diff --git a/src/reader.rs b/src/reader.rs index a1c3741..a1fd935 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -5,7 +5,7 @@ use serialize::Decodable; use buffered::BufferedReader; use { - ByteString, CsvResult, Decoded, + ByteString, CsvResult, Decoded, IntoVector, Error, ErrDecode, ErrIo, ErrParse, ParseError, ParseErrorKind, UnequalLengths, @@ -349,7 +349,7 @@ impl Reader { } /// Creates a CSV reader for an in memory buffer of bytes. - pub fn from_bytes>(bytes: V) -> Reader { + pub fn from_bytes>(bytes: V) -> Reader { Reader::from_reader(MemReader::new(bytes.into_vec())) } } diff --git a/src/test.rs b/src/test.rs index 1e8869c..54cf68a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,6 @@ use std::io::Reader as IoReader; use std::io::Writer as IoWriter; -use {Reader, Writer, ByteString, CsvResult, collect}; +use {Reader, Writer, ByteString, CsvResult, collect, IntoVector}; fn ordie(res: Result) -> T { match res { @@ -9,7 +9,7 @@ fn ordie(res: Result) -> T { } } -fn bytes>(bs: S) -> ByteString { +fn bytes>(bs: S) -> ByteString { ByteString::from_bytes(bs) }