Skip to content

Replace usage of push_all and resize with extend #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2015
Merged
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
9 changes: 4 additions & 5 deletions src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! This consumer will take 4 samples from the input, print them, then stop
//!
//! ```rust
//! #![feature(io)]
//! use nom::{IResult,Needed,MemProducer,Consumer,ConsumerState};
//! use std::str;
//! use std::io::SeekFrom;
Expand Down Expand Up @@ -113,7 +112,7 @@ pub trait Consumer {
let mut tmp = Vec::new();
//println!("before:\n{}", acc.to_hex(16));
//println!("after:\n{}", (&acc[consumed..acc.len()]).to_hex(16));
tmp.push_all(&acc[consumed..acc.len()]);
tmp.extend(acc[consumed..acc.len()].iter().cloned());
acc.clear();
acc = tmp;
} else {
Expand All @@ -124,7 +123,7 @@ pub trait Consumer {
acc.clear();
} else {
let mut tmp = Vec::new();
tmp.push_all(&acc[consumed..acc.len()]);
tmp.extend(acc[consumed..acc.len()].iter().cloned());
acc.clear();
acc = tmp;
}
Expand All @@ -134,7 +133,7 @@ pub trait Consumer {
match state {
Data(v) => {
//println!("got data: {} bytes", v.len());
acc.push_all(v);
acc.extend(v.iter().cloned());
position = position + v.len();
},
Eof(v) => {
Expand All @@ -146,7 +145,7 @@ pub trait Consumer {
} else {
//println!("eof with {} bytes", v.len());
eof = true;
acc.push_all(v);
acc.extend(v.iter().cloned());
position = position + v.len();
break;
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
//! ```
//!

#![feature(collections)]

pub use self::util::*;
pub use self::internal::*;//{IResult, IResultClosure, GetInput, GetOutput};
pub use self::map::*;
Expand Down
2 changes: 0 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate collections;

#[macro_export]
macro_rules! closure (
($ty:ty, $submac:ident!( $($args:tt)* )) => (
Expand Down
2 changes: 0 additions & 2 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//! but the macros system makes no promises.
//!

extern crate collections;

use std::fmt::Debug;
use internal::*;
use internal::IResult::*;
Expand Down
12 changes: 7 additions & 5 deletions src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::fs::File;
use std::path::Path;
use std::io;
use std::io::{Read,Seek,SeekFrom};
use std::iter::repeat;

/// Holds the data producer's current state
///
Expand Down Expand Up @@ -78,9 +79,10 @@ impl FileProducer {

impl Producer for FileProducer {
fn produce(&mut self) -> ProducerState<&[u8]> {
let len = self.v.len();
//let mut v = Vec::with_capacity(self.size);
//self.v.clear();
self.v.resize(self.size, 0);
self.v.extend(repeat(0).take(self.size - len));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm making the assumption here that resize only every grows the vector. I might be mistaken there - I only glanced at the surroundings.

If I'm mistaken, I'll add the "grow or shrink?"-dance.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it can only grow the vector

match self.file.read(&mut self.v) {
Err(e) => {
//println!("producer error: {:?}", e);
Expand Down Expand Up @@ -217,21 +219,21 @@ macro_rules! pusher (
match state {
ProducerState::Data(v) => {
//println!("got data");
acc.push_all(v)
acc.extend(v.iter().cloned())
},
ProducerState::Eof(v) => {
if v.is_empty() {
//println!("eof empty, acc contains {} bytes: {:?}", acc.len(), acc);
break;
} else {
//println!("eof with {} bytes", v.len());
acc.push_all(v)
acc.extend(v.iter().cloned())
}
}
_ => {break;}
}
let mut v2: Vec<u8> = Vec::new();
v2.push_all(&acc[..]);
v2.extend(acc[..].iter().cloned());
//let p = IResult::Done(b"", v2.as_slice());
match $f(&v2[..]) {
IResult::Error(e) => {
Expand All @@ -244,7 +246,7 @@ macro_rules! pusher (
IResult::Done(i, _) => {
//println!("data, done");
acc.clear();
acc.push_all(i);
acc.extend(i.iter().cloned());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/mp4.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(log_syntax,trace_macros)]
#![allow(dead_code)]

#[macro_use]
Expand Down
2 changes: 0 additions & 2 deletions tests/test1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(collections,slice_patterns)]

#[macro_use]
extern crate nom;

Expand Down