binator/utils/
try_acc.rs

1use crate::utils::TryPush;
2
3/// This is very usefull to be use on combinator like try_fold.
4/// For example, `.try_fold_bounds(.., || Ok(Vec::new), TryAcc::try_acc)`.
5pub trait TryAcc: Sized {
6  /// The error returned by the collection if push fail
7  type Error;
8  /// Item stocked in the collection
9  type Item;
10
11  /// Try to accumulate item into Self. For example, for a vector that simply a
12  /// try_push.
13  fn try_acc(self, item: Self::Item) -> Result<Self, Self::Error>;
14}
15
16impl<T> TryAcc for T
17where
18  Self: TryPush,
19{
20  type Error = <T as TryPush>::Error;
21  type Item = <T as TryPush>::Item;
22
23  fn try_acc(mut self, item: Self::Item) -> Result<Self, Self::Error> {
24    match self.try_push(item).map(|_| ()) {
25      Ok(_) => Ok(self),
26      Err(e) => Err(e),
27    }
28  }
29}