Created
July 21, 2021 00:29
-
-
Save scooter-dangle/e759497c0116865785e7a9f328cbbd6f to your computer and use it in GitHub Desktop.
Playing around at the July 20 Rust DC meetup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::Arc; | |
// #[derive(Debug)] | |
// struct Database(Vec<User>); | |
// #[derive(Debug, Default)] | |
// struct User { | |
// id: usize, | |
// login: UserLogin, | |
// password: String, | |
// } | |
// | |
// #[derive(Debug, Clone, Default)] | |
// struct UserLogin(String); | |
// | |
// impl User { | |
// pub fn new() -> Self { | |
// Self::default() | |
// } | |
// | |
// pub fn login(&self) -> &UserLogin { | |
// &self.login | |
// } | |
// | |
// pub fn external_login<'a, 'b: 'a>(&'a self, external: Option<&'b UserLogin>) -> &'a UserLogin { | |
// external.unwrap_or(&self.login) | |
// } | |
// | |
// pub fn external_login_str<'a, 'b: 'a>(&'a self, external: Option<&'b UserLogin>) -> &'a str { | |
// // external.unwrap_or(&self.login).0.as_str() | |
// let billy_bob: &'static str = "billy-bob"; | |
// billy_bob | |
// } | |
// | |
// pub fn is_good_login(&self, good_matcher: &str) -> bool { | |
// self.login().0 == good_matcher | |
// } | |
// } | |
#[derive(Debug)] | |
struct User<'a> { | |
id: usize, | |
login: UserLogin<'a>, | |
password: String, | |
} | |
#[derive(Debug, Clone)] | |
struct UserLogin<'a>(&'a str); | |
impl<'a> UserLogin<'a> { | |
fn new(login: &'a str) -> Option<Self> { | |
(login.len() >= 10 && login.len() <= 12).then(|| Self(login)) | |
} | |
} | |
impl<'a> User<'a> { | |
// pub fn new() -> Self { | |
// Self::default() | |
// } | |
// pub fn login(&self) -> &UserLogin { | |
// &self.login | |
// } | |
// pub fn external_login<'a, 'b: 'a>(&'a self, external: Option<&'b UserLogin>) -> &'a UserLogin { | |
// external.unwrap_or(&self.login) | |
// } | |
// pub fn external_login_str<'a, 'b: 'a>(&'a self, external: Option<&'b UserLogin>) -> &'a str { | |
// // external.unwrap_or(&self.login).0.as_str() | |
// let billy_bob: &'static str = "billy-bob"; | |
// billy_bob | |
// } | |
// pub fn is_good_login(&self, good_matcher: &str) -> bool { | |
// self.login().0 == good_matcher | |
// } | |
} | |
#[derive(Default)] | |
struct Metrics { | |
actual: Arc<()>, | |
sample_counter: usize, | |
} | |
impl Metrics { | |
fn sample(&mut self, denominator: usize) -> Sampler { | |
self.sample_counter += 1; | |
Sampler { | |
actual: (self.sample_counter % denominator == 0).then(|| self), | |
} | |
} | |
} | |
#[must_use] | |
struct Sampler<'a> { | |
actual: Option<&'a mut Metrics>, | |
} | |
impl<'a> Sampler<'a> { | |
fn my_thingy(self) { | |
if let Some(actual) = self.actual { | |
println!("{}", actual.sample_counter); | |
} | |
} | |
} | |
fn main() { | |
let potential_login = String::from("billy-bob22"); | |
let login = UserLogin::new(&potential_login).unwrap(); | |
let mut metrics = Metrics::default(); | |
let sample1 = metrics.sample(2); | |
sample1.my_thingy(); | |
let sample2 = metrics.sample(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment