Struct FuzzedDataProvider

Source
pub struct FuzzedDataProvider<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> FuzzedDataProvider<'a>

Source

pub fn new(data: &'a [u8]) -> Self

Creates a new FuzzedDataProvider

Source

pub fn remaining_bytes(&self) -> usize

Returns the number of remaining bytes the provider has

returns: The number of remaining bytes

Source

pub fn consume_bytes(&mut self, num_bytes: usize) -> Vec<u8>

Consumes num_bytes from the provider and returns them. If the provider doesn’t have num_bytes remaining, then it will return the remaining bytes

§Arguments
  • num_bytes: The number of bytes to consume

returns: The bytes from the provider

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[1, 2, 3, 4]);
    assert_eq!(fp.consume_bytes(2), vec![1, 2]);
}
Source

pub fn consume_bytes_as_string(&mut self, num_bytes: usize) -> String

Consumes num_bytes from the provider and returns them as a String. If the provider doesn’t have num_bytes remaining, then it will return the remaining bytes

§Arguments
  • num_bytes: The number of bytes to consume

returns: The bytes from the provider as a String

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[b'A', b'B', b'C']);
    assert_eq!(fp.consume_bytes_as_string(2), "AB");
}
Source

pub fn consume_remaining_bytes(self) -> Vec<u8>

Consumes the data provider and returns the remaining bytes if any.

returns: The remaining bytes

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let fp = FuzzedDataProvider::new(&[1, 2, 3]);
    assert_eq!(fp.consume_remaining_bytes(), vec![1, 2, 3]);
}
Source

pub fn consume_remaining_bytes_as_string(self) -> String

Consumes the data provider and returns the remaining bytes if any as a String

returns: The remaining bytes from the provider as a String

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let fp = FuzzedDataProvider::new(&[b'A', b'B', b'C']);
    assert_eq!(fp.consume_remaining_bytes_as_string(), "ABC");
}
Source

pub fn consume_random_length_string(&mut self, max_len: usize) -> String

Consumes at most max_len bytes from the provider as a String. If a backslash () is encountered then the consuming will stop earlier. If provider has less bytes remaining than max_len then the number of remaining bytes will be consumed at most.

§Arguments
  • max_len: The maximum number of bytes to consume

returns: The bytes consumed as a String

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[b'A', b'B', b'\\', b'C', b'D']);
    assert_eq!(fp.consume_random_length_string(4), "AB");
    assert_eq!(fp.consume_random_length_string(3), "D");
}
Source

pub fn consume_bool(&mut self) -> bool

Consumes a bytes from the provider and returns that as a bool

returns: The byte consumed as a bool

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[2, 1]);
    assert_eq!(fp.consume_bool(), true);
    assert_eq!(fp.consume_bool(), false);
}
Source

pub fn consume_probability_f32(&mut self) -> f32

Consumes the required bytes from the provider and returns a probability as f32. Probability is a value between 0.0 and 1.0

returns: The probability value

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[255, 0]);
    assert!((0.0f32..1.0f32).contains(&fp.consume_probability_f32()));
}
Source

pub fn consume_probability_f64(&mut self) -> f64

Consumes the required bytes from the provider and returns a probability as f64. Probability is a value between 0.0 and 1.0

returns: The probability value

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let mut fp = FuzzedDataProvider::new(&[255, 0]);
    assert!((0.0f64..1.0f64).contains(&fp.consume_probability_f64()));
}
Source

pub fn pick_value_from<T: Clone, S: AsRef<[T]>>( &mut self, slice: S, ) -> Option<T>

Consumes std::mem::sizeof::<usize>() bytes from the provider constraining the value between 0 and slice.len() - 1. This value is then used as an index to the given slice and returns a copy of the value which belongs to that index.

§Arguments
  • slice: The slice to index and retrieve the value from

returns: The value from the slice if the slice is not empty or None otherwise.

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let values = [1, 2, 3, 4, 5];
    let mut fp = FuzzedDataProvider::new(&[2, 3]);
    assert_eq!(fp.pick_value_from(&values), Some(4));
    assert_eq!(fp.pick_value_from(&values), Some(3));
}
Source

pub fn pick_value_from_or_default<T: Clone + Default, S: AsRef<[T]>>( &mut self, slice: S, ) -> T

Consumes std::mem::sizeof::<usize>() bytes from the provider constraining the value between 0 and slice.len() - 1. This value is then used as an index to the given slice and returns a copy of the value which belongs to that index.

§Arguments
  • slice: The slice to index and retrieve the value from

returns: The value from the slice if the slice is not empty or T::default() otherwise.

§Examples
use fuzzed_data_provider_rs::FuzzedDataProvider;

fn main() {
    let values: &[u32] = &[];
    let mut fp = FuzzedDataProvider::new(&[2, 3]);
    assert_eq!(fp.pick_value_from_or_default(values), u32::default());
}
Source

pub fn consume_i8_in_range(&mut self, min: i8, max: i8) -> i8

Consumes std::mem::sizeof::<i8>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_u8_in_range(&mut self, min: u8, max: u8) -> u8

Consumes std::mem::sizeof::<u8>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_i16_in_range(&mut self, min: i16, max: i16) -> i16

Consumes std::mem::sizeof::<i16>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_u16_in_range(&mut self, min: u16, max: u16) -> u16

Consumes std::mem::sizeof::<u16>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_i32_in_range(&mut self, min: i32, max: i32) -> i32

Consumes std::mem::sizeof::<i32>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_u32_in_range(&mut self, min: u32, max: u32) -> u32

Consumes std::mem::sizeof::<u32>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_i64_in_range(&mut self, min: i64, max: i64) -> i64

Consumes std::mem::sizeof::<i64>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_u64_in_range(&mut self, min: u64, max: u64) -> u64

Consumes std::mem::sizeof::<u64>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_i128_in_range(&mut self, min: i128, max: i128) -> i128

Consumes std::mem::sizeof::<i128>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_u128_in_range(&mut self, min: u128, max: u128) -> u128

Consumes std::mem::sizeof::<u128>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_usize_in_range(&mut self, min: usize, max: usize) -> usize

Consumes std::mem::sizeof::<usize>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_isize_in_range(&mut self, min: isize, max: isize) -> isize

Consumes std::mem::sizeof::<isize>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_i8(&mut self) -> i8

Consumes std::mem::sizeof::<i8>() bytes from the provider an returns value in the range i8::MIN..i8::MAX

returns: The value between i8::MIN..i8::MAX

Source

pub fn consume_u8(&mut self) -> u8

Consumes std::mem::sizeof::<u8>() bytes from the provider an returns value in the range u8::MIN..u8::MAX

returns: The value between u8::MIN..u8::MAX

Source

pub fn consume_i16(&mut self) -> i16

Consumes std::mem::sizeof::<i16>() bytes from the provider an returns value in the range i16::MIN..i16::MAX

returns: The value between i16::MIN..i16::MAX

Source

pub fn consume_u16(&mut self) -> u16

Consumes std::mem::sizeof::<u16>() bytes from the provider an returns value in the range u16::MIN..u16::MAX

returns: The value between u16::MIN..u16::MAX

Source

pub fn consume_i32(&mut self) -> i32

Consumes std::mem::sizeof::<i32>() bytes from the provider an returns value in the range i32::MIN..i32::MAX

returns: The value between i32::MIN..i32::MAX

Source

pub fn consume_u32(&mut self) -> u32

Consumes std::mem::sizeof::<u32>() bytes from the provider an returns value in the range u32::MIN..u32::MAX

returns: The value between u32::MIN..u32::MAX

Source

pub fn consume_i64(&mut self) -> i64

Consumes std::mem::sizeof::<i64>() bytes from the provider an returns value in the range i64::MIN..i64::MAX

returns: The value between i64::MIN..i64::MAX

Source

pub fn consume_u64(&mut self) -> u64

Consumes std::mem::sizeof::<u64>() bytes from the provider an returns value in the range u64::MIN..u64::MAX

returns: The value between u64::MIN..u64::MAX

Source

pub fn consume_i128(&mut self) -> i128

Consumes std::mem::sizeof::<i128>() bytes from the provider an returns value in the range i128::MIN..i128::MAX

returns: The value between i128::MIN..i128::MAX

Source

pub fn consume_u128(&mut self) -> u128

Consumes std::mem::sizeof::<u128>() bytes from the provider an returns value in the range u128::MIN..u128::MAX

returns: The value between u128::MIN..u128::MAX

Source

pub fn consume_usize(&mut self) -> usize

Consumes std::mem::sizeof::<usize>() bytes from the provider an returns value in the range usize::MIN..usize::MAX

returns: The value between usize::MIN..usize::MAX

Source

pub fn consume_isize(&mut self) -> isize

Consumes std::mem::sizeof::<isize>() bytes from the provider an returns value in the range isize::MIN..isize::MAX

returns: The value between isize::MIN..isize::MAX

Source

pub fn consume_f32_in_range(&mut self, min: f32, max: f32) -> f32

Consumes std::mem::sizeof::<f32>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_f64_in_range(&mut self, min: f64, max: f64) -> f64

Consumes std::mem::sizeof::<f64>() bytes from the provider and returns a value in the range min..max

§Arguments:
  • min: The minimum value in the range
  • max: The maximum value in the range

returns: The value between min..max

Source

pub fn consume_f32(&mut self) -> f32

Consumes std::mem::sizeof::<f32>() bytes from the provider and returns a value in the range f32::MIN..f32::MAX

returns: The value between f32::MIN..f32::MAX

Source

pub fn consume_f64(&mut self) -> f64

Consumes std::mem::sizeof::<f64>() bytes from the provider and returns a value in the range f64::MIN..f64::MAX

returns: The value between f64::MIN..f64::MAX

Trait Implementations§

Source§

impl<'a> Debug for FuzzedDataProvider<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FuzzedDataProvider<'a>

§

impl<'a> RefUnwindSafe for FuzzedDataProvider<'a>

§

impl<'a> Send for FuzzedDataProvider<'a>

§

impl<'a> Sync for FuzzedDataProvider<'a>

§

impl<'a> Unpin for FuzzedDataProvider<'a>

§

impl<'a> UnwindSafe for FuzzedDataProvider<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.