Skip to content

Commit

Permalink
Replace Sex::gen with Rng::gen
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 5, 2023
1 parent 14a2d7a commit 4230550
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/infrastructure/namegen.rs
Expand Up @@ -136,7 +136,8 @@ async fn gen_names(sex: Sex) -> anyhow::Result<Names> {
impl GeneratePiUseCase for NamesCache {
#[tracing::instrument(skip(self), err, ret)]
async fn generate_pi(&self, kana_form: KanaForm) -> Result<PI, GenPiError> {
let sex = Sex::gen();
let mut rng = rand::rngs::OsRng;
let sex = rng.gen::<Sex>();
let name = NameGenerator::generate(self, sex)
.await
.map_err(GenPiError::GenNameError)?;
Expand Down
10 changes: 2 additions & 8 deletions src/model/sex.rs
@@ -1,18 +1,10 @@
use rand::{thread_rng, Rng};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Sex {
Female,
Male,
}

impl Sex {
pub fn gen() -> Self {
[Self::Female, Self::Male][thread_rng().gen_range(0..1)]
}
}

impl rand::distributions::Distribution<Sex> for rand::distributions::Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Sex {
if rng.gen::<bool>() {
Expand All @@ -27,6 +19,8 @@ impl rand::distributions::Distribution<Sex> for rand::distributions::Standard {
mod tests {
use std::collections::HashSet;

use rand::Rng;

use super::*;

#[test]
Expand Down

0 comments on commit 4230550

Please sign in to comment.