File: null.rs

package info (click to toggle)
rust-rmp 0.8.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::Cursor;

use rmp::decode::*;

#[test]
fn pass() {
    let buf = [0xc0];
    let mut cur = Cursor::new(&buf[..]);

    assert_eq!((), read_nil(&mut cur).unwrap());
    assert_eq!(1, cur.position());
}

#[test]
fn fail_invalid_marker() {
    let buf = [0xc1];
    let mut cur = Cursor::new(&buf[..]);

    match read_nil(&mut cur) {
        Err(ValueReadError::TypeMismatch(..)) => (),
        other => panic!("unexpected result: {other:?}"),
    }
    assert_eq!(1, cur.position());
}

#[test]
fn fail_unexpected_eof() {
    let buf = [];
    let mut cur = Cursor::new(&buf[..]);

    read_nil(&mut cur).err().unwrap();
    assert_eq!(0, cur.position());
}

#[test]
#[cfg(feature = "std")]
fn interrupt_safe() {
    use std::io::{Error, ErrorKind, Read};

    struct MockRead { state_: u8 }

    impl MockRead {
        fn state(&self) -> u8 { self.state_ }
    }

    impl Read for MockRead {
        fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
            if self.state_ == 0 {
                self.state_ = 1;
                Err(Error::new(ErrorKind::Interrupted, "interrupted"))
            } else {
                assert!(!buf.is_empty());

                buf[0] = 0xc0;
                Ok(1)
            }
        }
    }

    let mut cur = MockRead { state_: 0 };

    // The function is interruption-safe, the first read should succeed.
    read_nil(&mut cur).unwrap();

    assert_eq!(1, cur.state());
}