3 ms·
Isn't the idea with traits that you can pass in anything you want that implements the trait? I think a larger problem here is that the function takes Settings
by WirelessGigabit 3y ago
Isn't the idea with traits that you can pass in anything you want that implements the trait?
I think a larger problem here is that the function takes Settings and not a &Vec Option<&Vec>, that way you'd notice immediately.
Or you split the code and add type constraints:
struct ClientIdentifier {}
struct Settings {
allow_list: Vec<ClientIdentifier>
}
struct Settings2 {
allow_list: Option<Vec<ClientIdentifier>>
}
fn allow_list_count(settings: Settings) -> usize {
// clear you're iterating over a slice
let iter: std::slice::Iter<'_, _> = settings.allow_list.iter();
// and the count is len
iter.count()
}
fn allow_list_count_2(settings: Settings2) -> usize {
// clear that you're iterating over Option<T>
let iter: std::option::Iter<'_, _> = settings.allow_list.iter();
// which is 0 (None) or 1 (Some(T))
iter.count()
}