pub struct FuzzedDataProvider<'a> { /* private fields */ }
Implementations§
Source§impl<'a> FuzzedDataProvider<'a>
impl<'a> FuzzedDataProvider<'a>
Sourcepub fn remaining_bytes(&self) -> usize
pub fn remaining_bytes(&self) -> usize
Returns the number of remaining bytes the provider has
returns: The number of remaining bytes
Sourcepub fn consume_bytes(&mut self, num_bytes: usize) -> Vec<u8> ⓘ
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]);
}
Sourcepub fn consume_bytes_as_string(&mut self, num_bytes: usize) -> String
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");
}
Sourcepub fn consume_remaining_bytes(self) -> Vec<u8> ⓘ
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]);
}
Sourcepub fn consume_remaining_bytes_as_string(self) -> String
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");
}
Sourcepub fn consume_random_length_string(&mut self, max_len: usize) -> String
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");
}
Sourcepub fn consume_bool(&mut self) -> bool
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);
}
Sourcepub fn consume_probability_f32(&mut self) -> f32
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()));
}
Sourcepub fn consume_probability_f64(&mut self) -> f64
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()));
}
Sourcepub fn pick_value_from<T: Clone, S: AsRef<[T]>>(
&mut self,
slice: S,
) -> Option<T>
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));
}
Sourcepub fn pick_value_from_or_default<T: Clone + Default, S: AsRef<[T]>>(
&mut self,
slice: S,
) -> T
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());
}
Sourcepub fn consume_i8_in_range(&mut self, min: i8, max: i8) -> i8
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_u8_in_range(&mut self, min: u8, max: u8) -> u8
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_i16_in_range(&mut self, min: i16, max: i16) -> i16
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_u16_in_range(&mut self, min: u16, max: u16) -> u16
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_i32_in_range(&mut self, min: i32, max: i32) -> i32
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_u32_in_range(&mut self, min: u32, max: u32) -> u32
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_i64_in_range(&mut self, min: i64, max: i64) -> i64
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_u64_in_range(&mut self, min: u64, max: u64) -> u64
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_i128_in_range(&mut self, min: i128, max: i128) -> i128
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_u128_in_range(&mut self, min: u128, max: u128) -> u128
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_usize_in_range(&mut self, min: usize, max: usize) -> usize
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_isize_in_range(&mut self, min: isize, max: isize) -> isize
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_i8(&mut self) -> i8
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
Sourcepub fn consume_u8(&mut self) -> u8
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
Sourcepub fn consume_i16(&mut self) -> i16
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
Sourcepub fn consume_u16(&mut self) -> u16
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
Sourcepub fn consume_i32(&mut self) -> i32
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
Sourcepub fn consume_u32(&mut self) -> u32
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
Sourcepub fn consume_i64(&mut self) -> i64
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
Sourcepub fn consume_u64(&mut self) -> u64
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
Sourcepub fn consume_i128(&mut self) -> i128
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
Sourcepub fn consume_u128(&mut self) -> u128
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
Sourcepub fn consume_usize(&mut self) -> usize
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
Sourcepub fn consume_isize(&mut self) -> isize
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
Sourcepub fn consume_f32_in_range(&mut self, min: f32, max: f32) -> f32
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_f64_in_range(&mut self, min: f64, max: f64) -> f64
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 rangemax
: The maximum value in the range
returns: The value between min..max
Sourcepub fn consume_f32(&mut self) -> f32
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
Sourcepub fn consume_f64(&mut self) -> f64
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