Skip to main content

BufReadExt

Trait BufReadExt 

Source
pub trait BufReadExt: BufRead {
    // Provided methods
    fn read_while<P>(
        &mut self,
        buf: &mut Vec<u8>,
        predicate: P,
    ) -> Result<usize>
       where P: FnMut(u8) -> bool { ... }
    fn skip(&mut self, n: usize) -> Result<()> { ... }
    fn skip_while<P>(&mut self, predicate: P) -> Result<usize>
       where P: FnMut(u8) -> bool { ... }
    fn skip_until(&mut self, byte: u8) -> Result<usize> { ... }
}
Expand description

Extend BufRead with methods for streaming parsing.

Provided Methods§

Source

fn read_while<P>(&mut self, buf: &mut Vec<u8>, predicate: P) -> Result<usize>
where P: FnMut(u8) -> bool,

Read bytes based on a predicate.

read_while takes a predicate as an argument. It will call this on each byte, and copy it to the slice if the predicate evaluates to true. Returns the amount of bytes read.

§Errors

If this function encounters an error of the kind ErrorKind::Interrupted then the error is ignored and the operation will continue.

If any other read error is encountered then this function immediately returns. Any bytes which have already been read will be appended to buf.

§Examples

[std::io::Cursor][Cursor] is a type that implements BufRead. In this example, we use [Cursor] to read bytes in a byte slice until we encounter a hyphen:

use std::io::{self, BufRead};
use omnom::prelude::*;

let mut cursor = io::Cursor::new(b"lorem-ipsum");
let mut buf = vec![];

let num_bytes = cursor.read_while(&mut buf, |b| b != b'-')
    .expect("reading from cursor won't fail");
assert_eq!(buf, b"lorem");
Examples found in repository?
examples/mime.rs (line 96)
34fn parse_mime(s: &str) -> Option<Mime> {
35    // parse the "type"
36    //
37    // ```txt
38    // text/html; charset=utf-8;
39    // ^^^^^
40    // ```
41    let mut s = Cursor::new(s);
42    let mut base_type = vec![];
43    match s.read_until(b'/', &mut base_type).unwrap() {
44        0 => return None,
45        _ => base_type.pop(),
46    };
47    validate_code_points(&base_type)?;
48
49    // parse the "subtype"
50    //
51    // ```txt
52    // text/html; charset=utf-8;
53    //      ^^^^^
54    // ```
55    let mut sub_type = vec![];
56    s.read_until(b';', &mut sub_type).unwrap();
57    if let Some(b';') = sub_type.last() {
58        sub_type.pop();
59    }
60    validate_code_points(&sub_type)?;
61
62    // instantiate our mime struct
63    let mut mime = Mime {
64        base_type: String::from_utf8(base_type).unwrap(),
65        sub_type: String::from_utf8(sub_type).unwrap(),
66        parameters: None,
67    };
68
69    // parse parameters into a hashmap
70    //
71    // ```txt
72    // text/html; charset=utf-8;
73    //           ^^^^^^^^^^^^^^^
74    // ```
75    loop {
76        // Stop parsing if there's no more bytes to consume.
77        if s.fill_buf().unwrap().len() == 0 {
78            break;
79        }
80
81        // Trim any whitespace.
82        //
83        // ```txt
84        // text/html; charset=utf-8;
85        //           ^
86        // ```
87        s.skip_while(is_http_whitespace_char).ok()?;
88
89        // Get the param name.
90        //
91        // ```txt
92        // text/html; charset=utf-8;
93        //            ^^^^^^^
94        // ```
95        let mut param_name = vec![];
96        s.read_while(&mut param_name, |b| b != b';' && b != b'=')
97            .ok()?;
98        validate_code_points(&param_name)?;
99        let mut param_name = String::from_utf8(param_name).ok()?;
100        param_name.make_ascii_lowercase();
101
102        // Ignore param names without values.
103        //
104        // ```txt
105        // text/html; charset=utf-8;
106        //                   ^
107        // ```
108        let mut token = vec![0; 1];
109        s.read_exact(&mut token).unwrap();
110        if token[0] == b';' {
111            continue;
112        }
113
114        // Get the param value.
115        //
116        // ```txt
117        // text/html; charset=utf-8;
118        //                    ^^^^^^
119        // ```
120        let mut param_value = vec![];
121        s.read_until(b';', &mut param_value).ok()?;
122        if let Some(b';') = param_value.last() {
123            param_value.pop();
124        }
125        validate_code_points(&param_value)?;
126        let mut param_value = String::from_utf8(param_value).ok()?;
127        param_value.make_ascii_lowercase();
128
129        // Insert attribute pair into hashmap.
130        if let None = mime.parameters {
131            mime.parameters = Some(HashMap::new());
132        }
133        mime.parameters.as_mut()?.insert(param_name, param_value);
134    }
135
136    Some(mime)
137}
Source

fn skip(&mut self, n: usize) -> Result<()>

Skip the first n bytes.

Source

fn skip_while<P>(&mut self, predicate: P) -> Result<usize>
where P: FnMut(u8) -> bool,

Skip bytes while the predicate is true.

Examples found in repository?
examples/mime.rs (line 87)
34fn parse_mime(s: &str) -> Option<Mime> {
35    // parse the "type"
36    //
37    // ```txt
38    // text/html; charset=utf-8;
39    // ^^^^^
40    // ```
41    let mut s = Cursor::new(s);
42    let mut base_type = vec![];
43    match s.read_until(b'/', &mut base_type).unwrap() {
44        0 => return None,
45        _ => base_type.pop(),
46    };
47    validate_code_points(&base_type)?;
48
49    // parse the "subtype"
50    //
51    // ```txt
52    // text/html; charset=utf-8;
53    //      ^^^^^
54    // ```
55    let mut sub_type = vec![];
56    s.read_until(b';', &mut sub_type).unwrap();
57    if let Some(b';') = sub_type.last() {
58        sub_type.pop();
59    }
60    validate_code_points(&sub_type)?;
61
62    // instantiate our mime struct
63    let mut mime = Mime {
64        base_type: String::from_utf8(base_type).unwrap(),
65        sub_type: String::from_utf8(sub_type).unwrap(),
66        parameters: None,
67    };
68
69    // parse parameters into a hashmap
70    //
71    // ```txt
72    // text/html; charset=utf-8;
73    //           ^^^^^^^^^^^^^^^
74    // ```
75    loop {
76        // Stop parsing if there's no more bytes to consume.
77        if s.fill_buf().unwrap().len() == 0 {
78            break;
79        }
80
81        // Trim any whitespace.
82        //
83        // ```txt
84        // text/html; charset=utf-8;
85        //           ^
86        // ```
87        s.skip_while(is_http_whitespace_char).ok()?;
88
89        // Get the param name.
90        //
91        // ```txt
92        // text/html; charset=utf-8;
93        //            ^^^^^^^
94        // ```
95        let mut param_name = vec![];
96        s.read_while(&mut param_name, |b| b != b';' && b != b'=')
97            .ok()?;
98        validate_code_points(&param_name)?;
99        let mut param_name = String::from_utf8(param_name).ok()?;
100        param_name.make_ascii_lowercase();
101
102        // Ignore param names without values.
103        //
104        // ```txt
105        // text/html; charset=utf-8;
106        //                   ^
107        // ```
108        let mut token = vec![0; 1];
109        s.read_exact(&mut token).unwrap();
110        if token[0] == b';' {
111            continue;
112        }
113
114        // Get the param value.
115        //
116        // ```txt
117        // text/html; charset=utf-8;
118        //                    ^^^^^^
119        // ```
120        let mut param_value = vec![];
121        s.read_until(b';', &mut param_value).ok()?;
122        if let Some(b';') = param_value.last() {
123            param_value.pop();
124        }
125        validate_code_points(&param_value)?;
126        let mut param_value = String::from_utf8(param_value).ok()?;
127        param_value.make_ascii_lowercase();
128
129        // Insert attribute pair into hashmap.
130        if let None = mime.parameters {
131            mime.parameters = Some(HashMap::new());
132        }
133        mime.parameters.as_mut()?.insert(param_name, param_value);
134    }
135
136    Some(mime)
137}
Source

fn skip_until(&mut self, byte: u8) -> Result<usize>

Skip bytes until the delimiter byte or EOF is reached.

This function will read bytes from the underlying stream until the delimiter or EOF is found. Once found, all bytes up to, and including, the delimiter (if found) will be skipped.

If successful, this function will return the total number of bytes read.

§Errors

This function will ignore all instances of ErrorKind::Interrupted and will otherwise return any errors returned by BufRead::fill_buf.

If an I/O error is encountered then all bytes read so far will be present in buf and its length will have been adjusted appropriately.

§Examples

std::io::Cursor is a type that implements BufRead. In this example, we use Cursor to read all the bytes in a byte slice in hyphen delimited segments:

use std::io::{self, BufRead, Read};
use omnom::prelude::*;

let mut cursor = io::Cursor::new(b"lorem-ipsum");

// skip up to and including '-'
let num_bytes = cursor.skip_until(b'-').unwrap();
assert_eq!(num_bytes, 6);

// read the rest of the bytes
let mut buf = [0; 5];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"ipsum");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§