malachite_base/nevers/
mod.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use core::fmt::{Display, Formatter};
10use core::iter::{Empty, empty};
11use core::str::FromStr;
12
13/// `Never` is a type that cannot be instantiated.
14///
15/// This is a [bottom type](https://en.wikipedia.org/wiki/Bottom_type).
16///
17/// # Examples
18/// ```
19/// use malachite_base::nevers::Never;
20///
21/// let _x: Option<Never> = None;
22/// ```
23#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
24pub enum Never {}
25
26impl Display for Never {
27    /// Would convert a [`Never`] to a [`String`].
28    fn fmt(&self, _f: &mut Formatter) -> core::fmt::Result {
29        unreachable!()
30    }
31}
32
33#[derive(Clone, Copy, Debug, Eq, PartialEq)]
34pub struct NeverError;
35
36impl FromStr for Never {
37    type Err = NeverError;
38
39    /// Would convert a [`String`] to a [`Never`].
40    ///
41    /// Since a [`Never`] can never be instantiated, `from_str` never succeeds.
42    ///
43    /// # Worst-case complexity
44    /// Constant time and additional memory.
45    ///
46    /// # Examples
47    /// ```
48    /// use malachite_base::nevers::{Never, NeverError};
49    /// use std::str::FromStr;
50    ///
51    /// assert_eq!(Never::from_str("abc"), Err(NeverError));
52    /// ```
53    #[inline]
54    fn from_str(_: &str) -> Result<Never, NeverError> {
55        Err(NeverError)
56    }
57}
58
59/// Generates all (none) of the [`Never`]s.
60///
61/// The output length is 0.
62///
63/// # Worst-case complexity per iteration
64/// Constant time and additional memory.
65///
66/// # Examples
67/// ```
68/// use itertools::Itertools;
69/// use malachite_base::nevers::nevers;
70///
71/// assert_eq!(nevers().collect_vec(), &[]);
72/// ```
73pub const fn nevers() -> Empty<Never> {
74    empty()
75}