Skip to content

Commit

Permalink
Define IntoVector to replace lost functionality in CloneableVector.
Browse files Browse the repository at this point in the history
Fixes #5.
  • Loading branch information
BurntSushi committed Oct 16, 2014
1 parent 2f70641 commit a4e9ca5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
16 changes: 15 additions & 1 deletion src/bytestr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ use std::fmt;
use std::hash;
use std::ops;

/// A trait that encapsulates a `Vec<T>` or a `&[T]`.
pub trait IntoVector<T> {
/// Convert the underlying value to a vector.
fn into_vec(self) -> Vec<T>;
}

impl<T> IntoVector<T> for Vec<T> {
fn into_vec(self) -> Vec<T> { self }
}

impl<'a, T: Clone> IntoVector<T> for &'a [T] {
fn into_vec(self) -> Vec<T> { self.to_vec() }
}

/// A type that represents unadulterated byte strings.
///
/// Byte strings represent *any* 8 bit character encoding. There are no
Expand All @@ -27,7 +41,7 @@ pub struct ByteString(Vec<u8>);

impl ByteString {
/// Create a new byte string from a vector or slice of bytes.
pub fn from_bytes<S: CloneableVector<u8>>(bs: S) -> ByteString {
pub fn from_bytes<S: IntoVector<u8>>(bs: S) -> ByteString {
ByteString(bs.into_vec())
}

Expand Down
4 changes: 2 additions & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -24,7 +24,7 @@ impl Encoded {
/// to access the raw CSV record.
pub fn unwrap(self) -> Vec<ByteString> { self.record }

fn push_bytes<S: CloneableVector<u8>>(&mut self, s: S) -> CsvResult<()> {
fn push_bytes<S: IntoVector<u8>>(&mut self, s: S) -> CsvResult<()> {
self.record.push(ByteString::from_bytes(s));
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -349,7 +349,7 @@ impl Reader<MemReader> {
}

/// Creates a CSV reader for an in memory buffer of bytes.
pub fn from_bytes<V: CloneableVector<u8>>(bytes: V) -> Reader<MemReader> {
pub fn from_bytes<V: IntoVector<u8>>(bytes: V) -> Reader<MemReader> {
Reader::from_reader(MemReader::new(bytes.into_vec()))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -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<T, E: ::std::fmt::Show>(res: Result<T, E>) -> T {
match res {
Expand All @@ -9,7 +9,7 @@ fn ordie<T, E: ::std::fmt::Show>(res: Result<T, E>) -> T {
}
}

fn bytes<S: CloneableVector<u8>>(bs: S) -> ByteString {
fn bytes<S: IntoVector<u8>>(bs: S) -> ByteString {
ByteString::from_bytes(bs)
}

Expand Down

0 comments on commit a4e9ca5

Please sign in to comment.