Skip to main content

core/
unit.rs

1/// Collapses all unit items from an iterator into one.
2///
3/// This is more useful when combined with higher-level abstractions, like
4/// collecting to a `Result<(), E>` where you only care about errors:
5///
6/// ```
7/// use std::io::*;
8/// let data = vec![1, 2, 3, 4, 5];
9/// let res: Result<()> = data.iter()
10///     .map(|x| writeln!(stdout(), "{x}"))
11///     .collect();
12/// assert!(res.is_ok());
13/// ```
14#[stable(feature = "unit_from_iter", since = "1.23.0")]
15impl FromIterator<()> for () {
16    #[ferrocene::prevalidated]
17    fn from_iter<I: IntoIterator<Item = ()>>(iter: I) -> Self {
18        iter.into_iter().for_each(|()| {})
19    }
20}
21
22pub(crate) trait IsUnit {
23    fn is_unit() -> bool;
24}
25
26impl<T: ?Sized> IsUnit for T {
27    #[ferrocene::prevalidated]
28    default fn is_unit() -> bool {
29        false
30    }
31}
32
33impl IsUnit for () {
34    #[ferrocene::prevalidated]
35    fn is_unit() -> bool {
36        true
37    }
38}