Skip to content

Commit 00a820b

Browse files
authored
Merge pull request #11 from bryceberger/all-to-slice
Change `all()` method to return `&'static []`
2 parents a82e64d + bd831e2 commit 00a820b

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

kinded/src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub trait Kinded {
1010
}
1111

1212
pub trait Kind: PartialEq + Eq + Debug + Clone + Copy {
13-
/// Return a vector with all possible kind variants.
14-
fn all() -> Vec<Self>;
13+
/// Return a slice with all possible kind variants.
14+
fn all() -> &'static [Self];
1515
}

kinded_macros/src/gen/kind_enum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn gen_definition(meta: &Meta) -> TokenStream {
3131
} // }
3232

3333
impl #kind_name { // impl DrinkKind {
34-
pub fn all() -> Vec<#kind_name> { // pub fn all() -> Vec<DrinkKind> {
35-
vec![ // vec![
34+
pub fn all() -> &'static [#kind_name] { // pub fn all() -> &'static [DrinkKind] {
35+
&[ // &[
3636
#(#kind_name::#variant_names),* // DrinkKind::Mate, DrinkKind::Coffee, DrinkKind::Tea
3737
] // ]
3838
} // }
@@ -139,7 +139,7 @@ fn gen_impl_kind_trait(meta: &Meta) -> TokenStream {
139139

140140
quote!(
141141
impl ::kinded::Kind for #kind_name {
142-
fn all() -> Vec<#kind_name> {
142+
fn all() -> &'static [#kind_name] {
143143
Self::all()
144144
}
145145
}

test_suite/src/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,27 +275,20 @@ mod kind_enum {
275275
mod kind_trait {
276276
use crate::RoleKind;
277277

278-
fn get_all_kinds<T: kinded::Kind>() -> Vec<T> {
279-
T::all()
280-
}
281-
282278
#[test]
283279
fn should_implement_kind_trait() {
284-
let kinds = get_all_kinds::<RoleKind>();
285280
assert_eq!(
286-
kinds,
287-
vec![RoleKind::Guest, RoleKind::User, RoleKind::Admin]
281+
RoleKind::all(),
282+
[RoleKind::Guest, RoleKind::User, RoleKind::Admin]
288283
)
289284
}
290285
}
291286
}
292287

293288
#[test]
294289
fn should_provide_all_function_that_returns_iterator() {
295-
assert_eq!(
296-
RoleKind::all(),
297-
vec![RoleKind::Guest, RoleKind::User, RoleKind::Admin],
298-
)
290+
fn impl_iter(_: impl IntoIterator<Item = &'static RoleKind>) {}
291+
impl_iter(RoleKind::all());
299292
}
300293
}
301294

0 commit comments

Comments
 (0)