use std::thread;
use std::time::Duration;
pub fn simulated_expensive_calculation(intensity: u32) -> u32{
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
intensity
}
pub struct Cacher<T>
where T: Fn(u32) -> u32
{
calculation: T,
value: Option<u32>,
}
impl<T> Cacher<T>
where T: Fn(u32) -> u32
{
pub fn new(calculation: T) -> Cacher<T>{
Cacher { calculation, value: None, }
}
pub fn value(&mut self, arg: u32) -> u32{
match self.value {
Some(v) => v,
None => {
let v = (self.calculation)(arg);
self.value = Some(v);
v
},
}
}
}
#[derive(PartialEq, Debug)]
pub struct Shoe{
size: u32,
style: String,
}
impl Shoe{
pub fn new(size: u32, style: String) -> Shoe{
Shoe { size, style, }
}
}
pub fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe>{
shoes.into_iter().filter(|s|s.size == shoe_size).collect()
}
pub struct Counter{
count: u32,
}
impl Counter{
pub fn new() -> Counter{
Counter{ count: 0, }
}
}
impl Iterator for Counter{
type Item = u32;
fn next(&mut self) -> Option<Self::Item>{
self.count += 1;
if self.count < 6{
Some(self.count)
}else{
None
}
}
}