-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlength.rs
More file actions
55 lines (49 loc) · 1.37 KB
/
length.rs
File metadata and controls
55 lines (49 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::generator::generate;
use crate::Syllable;
use rand::distr::{Distribution, StandardUniform};
use std::fmt;
use std::ops;
/// Pseudo-word of some specified minimum length N
///
/// Wrapper type around [`String`] which implements [`Distribution`](rand::distributions::Distribution)
#[derive(Debug)]
pub struct GabLength<const N: usize>(pub String);
impl<const N: usize> Distribution<GabLength<{ N }>> for StandardUniform {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> GabLength<{ N }> {
GabLength(generate(
rng,
Syllable::Alphabet,
Syllable::Alphabet,
Some(N),
))
}
}
impl<const N: usize> ops::Deref for GabLength<{ N }> {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<const N: usize> ops::DerefMut for GabLength<{ N }> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<const N: usize> fmt::Display for GabLength<{ N }> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
#[test]
pub fn gablength() {
use crate::GabLength;
use rand::rng;
use rand::RngExt;
let mut rng = rng();
let gib: GabLength<4> = rng.random();
assert!(!gib.is_empty());
println!("length {}", gib);
}
}