Start work on challenge 12.
This commit is contained in:
14
src/bytes.rs
14
src/bytes.rs
@@ -1,4 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
use rand::Rng;
|
||||
use std::fmt::Write; // need to import this trait
|
||||
|
||||
#[derive(PartialEq, PartialOrd, Debug)]
|
||||
@@ -15,6 +16,19 @@ impl Bytes {
|
||||
String::from(std::str::from_utf8(&v).unwrap())
|
||||
}
|
||||
|
||||
pub fn random(length: usize) -> Bytes {
|
||||
Bytes(
|
||||
(0..length)
|
||||
.map(|_| rand::thread_rng().gen_range(0..255))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn random_range(lower: usize, upper: usize) -> Bytes {
|
||||
let length: usize = rand::thread_rng().gen_range(lower..upper);
|
||||
Bytes::random(length)
|
||||
}
|
||||
|
||||
pub fn from_hex(s: &str) -> Bytes {
|
||||
if s.len() % 2 != 0 {
|
||||
panic!("Input string has uneven number of characters");
|
||||
|
||||
Reference in New Issue
Block a user