Make ProgressMsg use Vec.

This commit is contained in:
Ms2ger 2014-04-26 14:37:02 +02:00
parent cc7d04702d
commit 6e617d8eba
9 changed files with 27 additions and 24 deletions

View file

@ -53,14 +53,16 @@ fn load(url: Url, start_chan: Sender<LoadResponse>) {
progress_chan.send(Done(Err(())));
}
Ok(data) => {
progress_chan.send(Payload(data));
let data: ~[u8] = data;
progress_chan.send(Payload(data.move_iter().collect()));
progress_chan.send(Done(Ok(())));
}
}
} else {
// FIXME: Since the %-decoded URL is already a str, we can't
// handle UTF8-incompatible encodings.
progress_chan.send(Payload(parts[1].as_bytes().into_owned()));
let bytes: &[u8] = parts[1].as_bytes();
progress_chan.send(Payload(bytes.iter().map(|&x| x).collect()));
progress_chan.send(Done(Ok(())));
}
}
@ -69,7 +71,7 @@ fn load(url: Url, start_chan: Sender<LoadResponse>) {
fn assert_parse(url: &'static str,
content_type: Option<(~str, ~str)>,
charset: Option<~str>,
data: Option<~[u8]>) {
data: Option<Vec<u8>>) {
use std::from_str::FromStr;
use std::comm;
@ -100,35 +102,35 @@ fn empty_invalid() {
#[test]
fn plain() {
assert_parse("data:,hello%20world", None, None, Some(bytes!("hello world").into_owned()));
assert_parse("data:,hello%20world", None, None, Some(bytes!("hello world").iter().map(|&x| x).collect()));
}
#[test]
fn plain_ct() {
assert_parse("data:text/plain,hello",
Some((~"text", ~"plain")), None, Some(bytes!("hello").into_owned()));
Some((~"text", ~"plain")), None, Some(bytes!("hello").iter().map(|&x| x).collect()));
}
#[test]
fn plain_charset() {
assert_parse("data:text/plain;charset=latin1,hello",
Some((~"text", ~"plain")), Some(~"latin1"), Some(bytes!("hello").into_owned()));
Some((~"text", ~"plain")), Some(~"latin1"), Some(bytes!("hello").iter().map(|&x| x).collect()));
}
#[test]
fn base64() {
assert_parse("data:;base64,C62+7w==", None, None, Some(~[0x0B, 0xAD, 0xBE, 0xEF]));
assert_parse("data:;base64,C62+7w==", None, None, Some(vec!(0x0B, 0xAD, 0xBE, 0xEF)));
}
#[test]
fn base64_ct() {
assert_parse("data:application/octet-stream;base64,C62+7w==",
Some((~"application", ~"octet-stream")), None, Some(~[0x0B, 0xAD, 0xBE, 0xEF]));
Some((~"application", ~"octet-stream")), None, Some(vec!(0x0B, 0xAD, 0xBE, 0xEF)));
}
#[test]
fn base64_charset() {
assert_parse("data:text/plain;charset=koi8-r;base64,8PLl9+XkIO3l5Pfl5A==",
Some((~"text", ~"plain")), Some(~"koi8-r"),
Some(~[0xF0, 0xF2, 0xE5, 0xF7, 0xE5, 0xE4, 0x20, 0xED, 0xE5, 0xE4, 0xF7, 0xE5, 0xE4]));
Some(vec!(0xF0, 0xF2, 0xE5, 0xF7, 0xE5, 0xE4, 0x20, 0xED, 0xE5, 0xE4, 0xF7, 0xE5, 0xE4)));
}

View file

@ -16,7 +16,7 @@ fn read_all(reader: &mut io::Stream, progress_chan: &Sender<ProgressMsg>)
loop {
let mut buf = Vec::new();
match reader.push_exact(&mut buf, READ_SIZE) {
Ok(_) => progress_chan.send(Payload(buf.iter().map(|&x| x).collect())),
Ok(_) => progress_chan.send(Payload(buf)),
Err(e) => match e.kind {
io::EndOfFile => return Ok(()),
_ => return Err(()),

View file

@ -104,7 +104,8 @@ fn load(mut url: Url, start_chan: Sender<LoadResponse>) {
match response.read(buf) {
Ok(len) => {
unsafe { buf.set_len(len); }
progress_chan.send(Payload(buf));
let buf: ~[u8] = buf;
progress_chan.send(Payload(buf.move_iter().collect()));
}
Err(_) => {
progress_chan.send(Done(Ok(())));

View file

@ -21,8 +21,8 @@ pub fn Image(width: u32, height: u32, color_type: png::ColorType, data: ~[u8]) -
static TEST_IMAGE: &'static [u8] = include_bin!("test.jpeg");
pub fn test_image_bin() -> ~[u8] {
TEST_IMAGE.into_owned()
pub fn test_image_bin() -> Vec<u8> {
TEST_IMAGE.iter().map(|&x| x).collect()
}
// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.

View file

@ -491,7 +491,7 @@ fn load_image_data(url: Url, resource_task: ResourceTask) -> Result<~[u8], ()> {
loop {
match progress_port.recv() {
resource_task::Payload(data) => {
image_data.push_all(data);
image_data.push_all(data.as_slice());
}
resource_task::Done(result::Ok(..)) => {
return Ok(image_data);
@ -554,7 +554,7 @@ mod tests {
struct SendBogusImage;
impl Closure for SendBogusImage {
fn invoke(&self, response: Sender<resource_task::ProgressMsg>) {
response.send(resource_task::Payload(~[]));
response.send(resource_task::Payload(vec!()));
response.send(resource_task::Done(Ok(())));
}
}

View file

@ -78,7 +78,7 @@ pub struct LoadResponse {
#[deriving(Eq,Show)]
pub enum ProgressMsg {
/// Binary data - there may be multiple of these
Payload(~[u8]),
Payload(Vec<u8>),
/// Indicates loading is complete, either successfully or not
Done(Result<(), ()>)
}
@ -104,7 +104,7 @@ pub fn load_whole_resource(resource_task: &ResourceTask, url: Url)
let mut buf = ~[];
loop {
match response.progress_port.recv() {
Payload(data) => buf.push_all(data),
Payload(data) => buf.push_all(data.as_slice()),
Done(Ok(())) => return Ok((response.metadata, buf)),
Done(Err(e)) => return Err(e)
}
@ -228,7 +228,7 @@ static snicklefritz_payload: [u8, ..3] = [1, 2, 3];
fn snicklefritz_loader_factory() -> LoaderTask {
let f: LoaderTask = proc(url: Url, start_chan: Sender<LoadResponse>) {
let progress_chan = start_sending(start_chan, Metadata::default(url));
progress_chan.send(Payload(snicklefritz_payload.into_owned()));
progress_chan.send(Payload(Vec::from_slice(snicklefritz_payload)));
progress_chan.send(Done(Ok(())));
};
f
@ -244,7 +244,7 @@ fn should_delegate_to_scheme_loader() {
let response = start.recv();
let progress = response.progress_port;
assert!(progress.recv() == Payload(snicklefritz_payload.into_owned()));
assert!(progress.recv() == Payload(Vec::from_slice(snicklefritz_payload)));
assert!(progress.recv() == Done(Ok(())));
resource_task.send(Exit);
}

View file

@ -62,8 +62,8 @@ struct ProgressMsgPortIterator {
progress_port: Receiver<ProgressMsg>
}
impl Iterator<~[u8]> for ProgressMsgPortIterator {
fn next(&mut self) -> Option<~[u8]> {
impl Iterator<Vec<u8>> for ProgressMsgPortIterator {
fn next(&mut self) -> Option<Vec<u8>> {
match self.progress_port.recv() {
Payload(data) => Some(data),
Done(..) => None

View file

@ -493,7 +493,7 @@ pub fn parse_html(page: &Page,
match load_response.progress_port.recv() {
Payload(data) => {
debug!("received data");
parser.parse_chunk(data);
parser.parse_chunk(data.as_slice());
}
Done(Err(..)) => {
fail!("Failed to load page URL {:s}", url.to_str());

View file

@ -41,13 +41,13 @@ pub struct StyleRule {
impl Stylesheet {
pub fn from_bytes_iter<I: Iterator<~[u8]>>(
pub fn from_bytes_iter<I: Iterator<Vec<u8>>>(
mut input: I, base_url: Url, protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingRef>) -> Stylesheet {
let mut bytes = ~[];
// TODO: incremental decoding and tokinization/parsing
for chunk in input {
bytes.push_all(chunk)
bytes.push_all(chunk.as_slice())
}
Stylesheet::from_bytes(bytes, base_url, protocol_encoding_label, environment_encoding)
}