[][src]Struct dangerous::Input

#[must_use = "input must be consumed"]pub struct Input<'i> { /* fields omitted */ }

Input is an immutable wrapper around bytes to be processed.

It can only be created via dangerous::input() as so to clearly point out where untrusted / dangerous input is consumed.

It is used along with Reader to process the input.

Formatting

Input implements both fmt::Debug and fmt::Display with support for pretty printing. See InputDisplay for formatting options.

Implementations

impl<'i> Input<'i>[src]

#[must_use]pub const fn len(&self) -> usize[src]

Returns the underlying byte slice length.

#[must_use]pub const fn is_empty(&self) -> bool[src]

Returns true if the underlying byte slice length is zero.

#[must_use]pub const fn is_bound(&self) -> bool[src]

Returns true if the underlying byte slice is bound.

See Input::bound() for more documentation.

#[must_use]pub fn is_within(&self, parent: &Input<'_>) -> bool[src]

Returns true if the underlying byte slice for parent contains that of self in the same section of memory with no bounds out of range.

#[must_use]pub fn count(&self, needle: u8) -> usize[src]

Returns the occurrences of needle within the underlying byte slice.

It is recommended to enable the bytecount dependency when using this function for better performance.

pub fn bound(self) -> Self[src]

Returns self as a bound Input.

Bound Input carries the guarantee that it will not be extended in future passes and as a result will not produce RetryRequirements.

Example

use dangerous::{Invalid, ToRetryRequirement};

let error: Invalid = dangerous::input(b"1234")
    .bound()
    .read_partial(|r| r.take(5))
    .unwrap_err();

// If the input was not bound, this wouldn't be fatal.
assert!(error.is_fatal());

#[must_use]pub fn span_of(&self, parent: &Input<'_>) -> Option<Range<usize>>[src]

Returns Some(Range) with the start and end offsets of self within the parent. None is returned if self is not within in the parent.

Example

let parent = dangerous::input(&[1, 2, 3, 4]);
let sub_range = 1..2;
let sub = dangerous::input(&parent.as_dangerous()[sub_range.clone()]);

assert_eq!(sub.span_of(&parent), Some(sub_range))

#[must_use]pub fn span_of_non_empty(&self, parent: &Input<'_>) -> Option<Range<usize>>[src]

Returns Some(Range) with the start and end offsets of self within the parent. None is returned if self is not within in the parent or self is empty.

pub fn display(&self) -> InputDisplay<'i>[src]

Returns an InputDisplay for formatting.

pub fn read_all<F, T, E>(self, f: F) -> Result<T, E> where
    F: FnOnce(&mut Reader<'i, E>) -> Result<T, E>,
    E: WithContext<'i>,
    E: From<ExpectedLength<'i>>, 
[src]

Create a reader with the expectation all of the input is read.

Errors

Returns an error if either the provided function does, or there is trailing input.

pub fn read_partial<F, T, E>(self, f: F) -> Result<(T, Input<'i>), E> where
    F: FnOnce(&mut Reader<'i, E>) -> Result<T, E>,
    E: WithContext<'i>, 
[src]

Create a reader to read a part of the input and return the rest.

Errors

Returns an error if the provided function does.

pub fn read_infallible<F, T>(self, f: F) -> (T, Input<'i>) where
    F: FnOnce(&mut Reader<'i, Infallible>) -> T, 
[src]

Create a reader to read a part of the input and return the rest without any errors.

#[must_use]pub const fn as_dangerous(&self) -> &'i [u8][src]

Returns the underlying byte slice.

The naming of this function is to a degree hyperbole, and should not be necessarily taken as proof of something dangerous or memory unsafe. It is named this way simply for users to clearly note where the panic-free guarantees end when handling the input.

pub fn to_dangerous_non_empty<E>(&self) -> Result<&'i [u8], E> where
    E: From<ExpectedLength<'i>>, 
[src]

Returns the underlying byte slice if it is not empty.

See as_dangerous for naming.

Errors

Returns ExpectedLength if the input is empty.

pub fn to_dangerous_str<E>(&self) -> Result<&'i str, E> where
    E: From<ExpectedValid<'i>>, 
[src]

Decodes the underlying byte slice into a UTF-8 str slice.

See as_dangerous for naming.

If the underlying byte slice is known to be valid UTF-8 this is will a cheap operation, otherwise the bytes will be validated.

Errors

Returns ExpectedValid if the input is not valid UTF-8.

pub fn to_dangerous_non_empty_str<E>(&self) -> Result<&'i str, E> where
    E: From<ExpectedValid<'i>>,
    E: From<ExpectedLength<'i>>, 
[src]

Decodes the underlying byte slice into a UTF-8 str slice.

See as_dangerous for naming.

Errors

Returns ExpectedLength if the input is empty or ExpectedValid if the input is not valid UTF-8.

Trait Implementations

impl<'i> Clone for Input<'i>[src]

impl<'i> Debug for Input<'i>[src]

impl<'i> Display for Input<'i>[src]

impl<'i> NoInteriorMut for Input<'i>[src]

impl<'i> PartialEq<&'_ [u8]> for Input<'i>[src]

impl<'i> PartialEq<[u8]> for Input<'i>[src]

impl<'i> PartialEq<[u8]> for &Input<'i>[src]

impl<'i> PartialEq<Input<'i>> for Input<'i>[src]

Auto Trait Implementations

impl<'i> RefUnwindSafe for Input<'i>[src]

impl<'i> Send for Input<'i>[src]

impl<'i> Sync for Input<'i>[src]

impl<'i> Unpin for Input<'i>[src]

impl<'i> UnwindSafe for Input<'i>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Guarded for T where
    T: NoInteriorMut
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.