[−][src]Struct dangerous::Reader
A Reader
is created from and consumes a Input
.
You can only create a Reader
from Input
via Input::read_all()
or
Input::read_partial()
.
Implementations
impl<'i, E> Reader<'i, E>
[src]
pub fn context<C, F, O>(&mut self, context: C, f: F) -> Result<O, E> where
E: FromContext<'i>,
C: Context,
F: FnOnce(&mut Self) -> Result<O, E>,
[src]
E: FromContext<'i>,
C: Context,
F: FnOnce(&mut Self) -> Result<O, E>,
Mutably use the reader with a given context.
Errors
Returns any error returned by the provided function, and attaches the specified context to it.
pub fn peek_context<C, F, O>(&self, context: C, f: F) -> Result<O, E> where
E: FromContext<'i>,
C: Context,
F: FnOnce(&Self) -> Result<O, E>,
[src]
E: FromContext<'i>,
C: Context,
F: FnOnce(&Self) -> Result<O, E>,
Immutably use the reader with a given context.
Errors
Returns any error returned by the provided function, and attaches the specified context to it.
pub fn at_end(&self) -> bool
[src]
Returns true
if the reader has no more input to consume.
pub fn skip(&mut self, len: usize) -> Result<(), E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
pub fn skip_while<F>(&mut self, pred: F) -> usize where
F: FnMut(u8) -> bool,
[src]
F: FnMut(u8) -> bool,
Skip a length of input while a predicate check remains true.
Returns the length of input skipped.
Errors
Returns any error the provided function does.
pub fn try_skip_while<F>(&mut self, pred: F) -> Result<usize, E> where
E: FromContext<'i>,
F: FnMut(u8) -> Result<bool, E>,
[src]
E: FromContext<'i>,
F: FnMut(u8) -> Result<bool, E>,
Try skip a length of input while a predicate check remains successful and true.
Returns the length of input skipped.
Errors
Returns any error the provided function does.
pub fn take(&mut self, len: usize) -> Result<&'i Input, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
pub fn take_remaining(&mut self) -> &'i Input
[src]
Read all of the input left.
pub fn take_while<F>(&mut self, pred: F) -> &'i Input where
F: FnMut(u8) -> bool,
[src]
F: FnMut(u8) -> bool,
Read a length of input while a predicate check remains true.
pub fn try_take_while<F>(&mut self, pred: F) -> Result<&'i Input, E> where
E: FromContext<'i>,
F: FnMut(u8) -> Result<bool, E>,
[src]
E: FromContext<'i>,
F: FnMut(u8) -> Result<bool, E>,
Try read a length of input while a predicate check remains successful and true.
Errors
Returns any error the provided function does.
pub fn take_consumed<F>(&mut self, consumer: F) -> &'i Input where
E: FromContext<'i>,
F: FnMut(&mut Self),
[src]
E: FromContext<'i>,
F: FnMut(&mut Self),
Read a length of input that was successfully parsed.
pub fn try_take_consumed<F>(&mut self, consumer: F) -> Result<&'i Input, E> where
E: FromContext<'i>,
F: FnMut(&mut Self) -> Result<(), E>,
[src]
E: FromContext<'i>,
F: FnMut(&mut Self) -> Result<(), E>,
Try read a length of input that was successfully parsed.
Errors
Returns an error if the provided function does.
pub fn peek<F, O>(&self, len: usize, f: F) -> Result<O, E> where
E: From<ExpectedLength<'i>>,
F: FnOnce(&Input) -> O,
[src]
E: From<ExpectedLength<'i>>,
F: FnOnce(&Input) -> O,
pub fn try_peek<F, O>(&self, len: usize, f: F) -> Result<O, E> where
E: FromContext<'i>,
E: From<ExpectedLength<'i>>,
F: FnOnce(&'i Input) -> Result<O, E>,
O: 'static,
[src]
E: FromContext<'i>,
E: From<ExpectedLength<'i>>,
F: FnOnce(&'i Input) -> Result<O, E>,
O: 'static,
Try peek a length of input.
Errors
Returns an error if the required length cannot be fulfilled, or if the provided function returns one.
pub fn peek_u8(&self) -> Result<u8, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Returns the next byte in the input without mutating the reader.
Errors
Returns an error if the reader has no more input.
pub fn peek_eq(&self, bytes: &[u8]) -> bool
[src]
Returns true
if bytes
is next in the input.
pub fn consume(&mut self, bytes: &'i [u8]) -> Result<(), E> where
E: From<ExpectedLength<'i>>,
E: From<ExpectedValue<'i>>,
[src]
E: From<ExpectedLength<'i>>,
E: From<ExpectedValue<'i>>,
Consume expected bytes from the input.
Doesn't effect the internal state if the input couldn't be consumed.
Errors
Returns an error if the bytes could not be consumed from the input.
pub fn consume_u8(&mut self, byte: u8) -> Result<(), E> where
E: From<ExpectedLength<'i>>,
E: From<ExpectedValue<'i>>,
[src]
E: From<ExpectedLength<'i>>,
E: From<ExpectedValue<'i>>,
Consume expected bytes from the input.
Doesn't effect the internal state if the input couldn't be consumed.
Errors
Returns an error if the bytes could not be consumed from the input.
pub fn expect<F, O>(&mut self, expected: &'static str, f: F) -> Result<O, E> where
F: FnOnce(&mut Self) -> Option<O>,
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
[src]
F: FnOnce(&mut Self) -> Option<O>,
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
Expect a value to be read and returned as Some
.
Errors
Returns an error if the returned value was None
.
pub fn try_expect<F, O>(&mut self, expected: &'static str, f: F) -> Result<O, E> where
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
F: FnOnce(&mut Self) -> Result<Option<O>, E>,
[src]
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
F: FnOnce(&mut Self) -> Result<Option<O>, E>,
Expect a value to be read successfully and returned as Some
.
Errors
Returns an error if failed to read, or the returned value was None
.
pub fn try_expect_erased<F, O, R>(
&mut self,
expected: &'static str,
f: F
) -> Result<O, E> where
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
F: FnOnce(&mut Self) -> Result<O, R>,
R: ToRetryRequirement,
[src]
&mut self,
expected: &'static str,
f: F
) -> Result<O, E> where
E: FromContext<'i>,
E: From<ExpectedValid<'i>>,
F: FnOnce(&mut Self) -> Result<O, R>,
R: ToRetryRequirement,
Expect a value with any error's details erased except for an optional
RetryRequirement
.
This function is useful for reading custom/unsupported types easily without having to create custom errors.
Example
use std::net::Ipv4Addr; use dangerous::{Invalid, Expected, Error}; // Our custom reader function fn read_ipv4_addr<'i, E>(input: &'i dangerous::Input) -> Result<Ipv4Addr, E> where E: Error<'i>, { input.read_all(|r| { r.try_expect_erased("ipv4 addr", |i| { i.take_remaining() .to_dangerous_str() .and_then(|s| s.parse().map_err(|_| Invalid::fatal())) }) }) } let input = dangerous::input(b"192.168.1.x"); let error: Expected = read_ipv4_addr(input).unwrap_err(); // Prefer string input formatting println!("{:#}", error);
Errors
Returns an error if either the provided function does, or there is trailing input.
pub fn read_u8(&mut self) -> Result<u8, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
pub fn read_i8_le(&mut self) -> Result<i8, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded i8
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i8_be(&mut self) -> Result<i8, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded i8
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u16_le(&mut self) -> Result<u16, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded u16
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u16_be(&mut self) -> Result<u16, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded u16
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i16_le(&mut self) -> Result<i16, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded i16
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i16_be(&mut self) -> Result<i16, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded i16
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u32_le(&mut self) -> Result<u32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded u32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u32_be(&mut self) -> Result<u32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded u32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i32_le(&mut self) -> Result<i32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded i32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i32_be(&mut self) -> Result<i32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded i32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u64_le(&mut self) -> Result<u64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded u64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u64_be(&mut self) -> Result<u64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded u64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i64_le(&mut self) -> Result<i64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded i64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i64_be(&mut self) -> Result<i64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded i64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u128_le(&mut self) -> Result<u128, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded u128
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_u128_be(&mut self) -> Result<u128, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded u128
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i128_le(&mut self) -> Result<i128, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded i128
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_i128_be(&mut self) -> Result<i128, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded i128
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_f32_le(&mut self) -> Result<f32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded f32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_f32_be(&mut self) -> Result<f32, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded f32
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_f64_le(&mut self) -> Result<f64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a little-endian encoded f64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn read_f64_be(&mut self) -> Result<f64, E> where
E: From<ExpectedLength<'i>>,
[src]
E: From<ExpectedLength<'i>>,
Read a big-endian encoded f64
from the reader.
Errors
Returns an error if there is not sufficient input left to read.
pub fn error<T>(&mut self) -> Reader<'_, T>
[src]
Create a sub reader with a given error type.
Trait Implementations
Auto Trait Implementations
impl<'i, E> RefUnwindSafe for Reader<'i, E> where
E: RefUnwindSafe,
E: RefUnwindSafe,
impl<'i, E> Send for Reader<'i, E> where
E: Send,
E: Send,
impl<'i, E> Sync for Reader<'i, E> where
E: Sync,
E: Sync,
impl<'i, E> Unpin for Reader<'i, E> where
E: Unpin,
E: Unpin,
impl<'i, E> UnwindSafe for Reader<'i, E> where
E: UnwindSafe,
E: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,