You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
622 B
42 lines
622 B
4 years ago
|
//! Utils
|
||
|
|
||
|
pub trait NewCapacity: Sized
|
||
|
{
|
||
|
fn new() -> Self;
|
||
|
fn with_capacity(cap: usize) -> Self;
|
||
|
}
|
||
|
|
||
|
impl NewCapacity for String
|
||
|
{
|
||
|
fn new() -> Self
|
||
|
{
|
||
|
Self::new()
|
||
|
}
|
||
|
|
||
|
fn with_capacity(cap: usize) -> Self
|
||
|
{
|
||
|
Self::with_capacity(cap)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T> NewCapacity for Vec<T>
|
||
|
{
|
||
|
fn new() -> Self
|
||
|
{
|
||
|
Self::new()
|
||
|
}
|
||
|
|
||
|
fn with_capacity(cap: usize) -> Self
|
||
|
{
|
||
|
Self::with_capacity(cap)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn hint_cap<T: NewCapacity, I: Iterator>(iter: &I) -> T
|
||
|
{
|
||
|
match iter.size_hint() {
|
||
|
(0, Some(0)) | (0, None) => T::new(),
|
||
|
(_, Some(x)) | (x, _) => T::with_capacity(x)
|
||
|
}
|
||
|
}
|