-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
try_collect
into its own trait (#8)
- Loading branch information
1 parent
8fe3741
commit 784001f
Showing
6 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::FallibleVec; | ||
use alloc::{collections::TryReserveError, vec::Vec}; | ||
use core::alloc::Allocator; | ||
|
||
/// Fallible allocations equivalents for [`Iterator::collect`]. | ||
pub trait TryCollect<T> { | ||
/// Attempts to collect items from an iterator into a vector with the provided | ||
/// allocator. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #![feature(allocator_api)] | ||
/// # #[macro_use] extern crate fallible_vec; | ||
/// use fallible_vec::*; | ||
/// use std::alloc::System; | ||
/// | ||
/// let doubled = [1, 2, 3, 4, 5].map(|i| i * 2); | ||
/// let vec = doubled.try_collect_in(System)?; | ||
/// assert_eq!(vec, [2, 4, 6, 8, 10]); | ||
/// # Ok::<(), std::collections::TryReserveError>(()) | ||
/// ``` | ||
fn try_collect_in<A: Allocator>(self, alloc: A) -> Result<Vec<T, A>, TryReserveError>; | ||
|
||
/// Attempts to collect items from an iterator into a vector. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #![feature(allocator_api)] | ||
/// # #[macro_use] extern crate fallible_vec; | ||
/// use fallible_vec::*; | ||
/// | ||
/// let doubled = [1, 2, 3, 4, 5].map(|i| i * 2); | ||
/// let vec = doubled.try_collect()?; | ||
/// assert_eq!(vec, [2, 4, 6, 8, 10]); | ||
/// # Ok::<(), std::collections::TryReserveError>(()) | ||
/// ``` | ||
fn try_collect(self) -> Result<Vec<T>, TryReserveError>; | ||
} | ||
|
||
impl<T, I> TryCollect<T> for I | ||
where | ||
I: IntoIterator<Item = T>, | ||
{ | ||
fn try_collect_in<A: Allocator>(self, alloc: A) -> Result<Vec<T, A>, TryReserveError> { | ||
let mut vec = Vec::new_in(alloc); | ||
vec.try_extend(self.into_iter())?; | ||
Ok(vec) | ||
} | ||
|
||
fn try_collect(self) -> Result<Vec<T>, TryReserveError> { | ||
let mut vec = Vec::new(); | ||
vec.try_extend(self.into_iter())?; | ||
Ok(vec) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters