Skip to content

Commit 7b14dd3

Browse files
committed
Switch Words to be a boxed slice
Not sure this is actually what's needed because `Box::new` still allocates, but I've learned a bunch by doing this!
1 parent ecdf118 commit 7b14dd3

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/lib.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
extern crate alloc;
5959

6060
use alloc::{
61+
boxed::Box,
6162
string::{String, ToString},
6263
vec::Vec,
6364
};
@@ -74,7 +75,7 @@ pub fn petname(words: u8, separator: &str) -> String {
7475
}
7576

7677
/// A word list.
77-
pub type Words<'a> = Vec<&'a str>;
78+
pub type Words<'a> = Box<[&'a str]>;
7879

7980
/// Word lists and the logic to combine them into _petnames_.
8081
///
@@ -138,9 +139,9 @@ impl<'a> Petnames<'a> {
138139
/// The words are extracted from the given strings by splitting on whitespace.
139140
pub fn init(adjectives: &'a str, adverbs: &'a str, names: &'a str) -> Self {
140141
Self {
141-
adjectives: adjectives.split_whitespace().collect(),
142-
adverbs: adverbs.split_whitespace().collect(),
143-
names: names.split_whitespace().collect(),
142+
adjectives: adjectives.split_whitespace().collect::<Vec<_>>().into_boxed_slice(),
143+
adverbs: adverbs.split_whitespace().collect::<Vec<_>>().into_boxed_slice(),
144+
names: names.split_whitespace().collect::<Vec<_>>().into_boxed_slice(),
144145
}
145146
}
146147

@@ -165,9 +166,27 @@ impl<'a> Petnames<'a> {
165166
where
166167
F: FnMut(&str) -> bool,
167168
{
168-
self.adjectives.retain(|word| predicate(word));
169-
self.adverbs.retain(|word| predicate(word));
170-
self.names.retain(|word| predicate(word));
169+
self.adjectives = self
170+
.adjectives
171+
.iter()
172+
.cloned()
173+
.filter(|word| predicate(word))
174+
.collect::<Vec<_>>()
175+
.into_boxed_slice();
176+
self.adverbs = self
177+
.adverbs
178+
.iter()
179+
.cloned()
180+
.filter(|word| predicate(word))
181+
.collect::<Vec<_>>()
182+
.into_boxed_slice();
183+
self.names = self
184+
.names
185+
.iter()
186+
.cloned()
187+
.filter(|word| predicate(word))
188+
.collect::<Vec<_>>()
189+
.into_boxed_slice();
171190
}
172191

173192
/// Calculate the cardinality of this `Petnames`.
@@ -488,27 +507,36 @@ where
488507

489508
#[cfg(test)]
490509
mod tests {
510+
use super::Box;
491511
use alloc::vec;
492512

493513
#[test]
494514
fn lists_sequences_adverbs_adjectives_then_names() {
495-
let petnames = super::Petnames::init("adjective", "adverb", "name");
515+
let petnames = super::Petnames {
516+
adjectives: Box::new(["adjective"]),
517+
adverbs: Box::new(["adverb"]),
518+
names: Box::new(["name"]),
519+
};
496520
let mut lists = super::Lists::new(&petnames, 4);
497521
assert_eq!(super::Lists::Adverb(&petnames, 1), lists);
498-
assert_eq!(Some(&vec!["adverb"]), lists.next());
522+
assert_eq!(Some(&vec!["adverb"].into_boxed_slice()), lists.next());
499523
assert_eq!(super::Lists::Adverb(&petnames, 0), lists);
500-
assert_eq!(Some(&vec!["adverb"]), lists.next());
524+
assert_eq!(Some(&vec!["adverb"].into_boxed_slice()), lists.next());
501525
assert_eq!(super::Lists::Adjective(&petnames), lists);
502-
assert_eq!(Some(&vec!["adjective"]), lists.next());
526+
assert_eq!(Some(&vec!["adjective"].into_boxed_slice()), lists.next());
503527
assert_eq!(super::Lists::Name(&petnames), lists);
504-
assert_eq!(Some(&vec!["name"]), lists.next());
528+
assert_eq!(Some(&vec!["name"].into_boxed_slice()), lists.next());
505529
assert_eq!(super::Lists::Done, lists);
506530
assert_eq!(None, lists.next());
507531
}
508532

509533
#[test]
510534
fn lists_size_hint() {
511-
let petnames = super::Petnames::init("adjective", "adverb", "name");
535+
let petnames = super::Petnames {
536+
adjectives: Box::new(["adjective"]),
537+
adverbs: Box::new(["adverb"]),
538+
names: Box::new(["name"]),
539+
};
512540
let mut lists = super::Lists::new(&petnames, 3);
513541
assert_eq!((3, Some(3)), lists.size_hint());
514542
assert!(lists.next().is_some());

tests/basic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn default_petnames_has_non_zero_cardinality() {
4747
#[test]
4848
fn generate_uses_adverb_adjective_name() {
4949
let petnames = Petnames {
50-
adjectives: vec!["adjective"],
51-
adverbs: vec!["adverb"],
52-
names: vec!["name"],
50+
adjectives: vec!["adjective"].into_boxed_slice(),
51+
adverbs: vec!["adverb"].into_boxed_slice(),
52+
names: vec!["name"].into_boxed_slice(),
5353
};
5454
assert_eq!(petnames.generate(&mut StepRng::new(0, 1), 3, "-"), "adverb-adjective-name");
5555
}

0 commit comments

Comments
 (0)