Skip to main content

malachite_base/nevers/
mod.rs

1// Copyright © 2026 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 crate::strings::latex::ToLatex;
10use crate::strings::typst::ToTypst;
11use core::fmt::{Display, Formatter};
12use core::iter::{Empty, empty};
13use core::str::FromStr;
14
15/// `Never` is a type that cannot be instantiated.
16///
17/// This is a [bottom type](https://en.wikipedia.org/wiki/Bottom_type).
18///
19/// # Examples
20/// ```
21/// use malachite_base::nevers::Never;
22///
23/// let _x: Option<Never> = None;
24/// ```
25#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
26pub enum Never {}
27
28impl Display for Never {
29    /// Would convert a [`Never`] to a [`String`].
30    fn fmt(&self, _f: &mut Formatter) -> core::fmt::Result {
31        unreachable!()
32    }
33}
34
35impl ToLatex for Never {
36    /// Would write a [`Never`] as a LaTeX math-mode fragment.
37    ///
38    /// A [`Never`] cannot be instantiated, so this can never be called. The implementation exists
39    /// so that a type parameter bounded by [`ToLatex`] may be [`Never`], which is what lets
40    /// `Option<Never>` have a fragment of its own.
41    ///
42    /// # Worst-case complexity
43    /// Constant time and additional memory.
44    ///
45    /// # Examples
46    /// ```
47    /// use malachite_base::nevers::Never;
48    /// use malachite_base::strings::latex::ToLatex;
49    ///
50    /// // The only `Option<Never>` there is.
51    /// assert_eq!(None::<Never>.to_latex_string(), r"\bot");
52    /// ```
53    fn fmt_latex(&self, _f: &mut Formatter) -> core::fmt::Result {
54        unreachable!()
55    }
56}
57
58impl ToTypst for Never {
59    /// Would write a [`Never`] as a Typst math-mode fragment.
60    ///
61    /// A [`Never`] cannot be instantiated, so this can never be called. The implementation exists
62    /// so that a type parameter bounded by [`ToTypst`] may be [`Never`], which is what lets
63    /// `Option<Never>` have a fragment of its own.
64    ///
65    /// # Worst-case complexity
66    /// Constant time and additional memory.
67    ///
68    /// # Examples
69    /// ```
70    /// use malachite_base::nevers::Never;
71    /// use malachite_base::strings::typst::ToTypst;
72    ///
73    /// // The only `Option<Never>` there is.
74    /// assert_eq!(None::<Never>.to_typst_string(), "bot");
75    /// ```
76    fn fmt_typst(&self, _f: &mut Formatter) -> core::fmt::Result {
77        unreachable!()
78    }
79}
80
81#[derive(Clone, Copy, Debug, Eq, PartialEq)]
82pub struct NeverError;
83
84impl FromStr for Never {
85    type Err = NeverError;
86
87    /// Would convert a [`String`] to a [`Never`].
88    ///
89    /// Since a [`Never`] can never be instantiated, `from_str` never succeeds.
90    ///
91    /// # Worst-case complexity
92    /// Constant time and additional memory.
93    ///
94    /// # Examples
95    /// ```
96    /// use malachite_base::nevers::{Never, NeverError};
97    /// use std::str::FromStr;
98    ///
99    /// assert_eq!(Never::from_str("abc"), Err(NeverError));
100    /// ```
101    #[inline]
102    fn from_str(_: &str) -> Result<Self, NeverError> {
103        Err(NeverError)
104    }
105}
106
107/// Generates all (none) of the [`Never`]s.
108///
109/// The output length is 0.
110///
111/// # Worst-case complexity per iteration
112/// Constant time and additional memory.
113///
114/// # Examples
115/// ```
116/// use itertools::Itertools;
117/// use malachite_base::nevers::nevers;
118///
119/// assert_eq!(nevers().collect_vec(), &[]);
120/// ```
121#[inline]
122pub const fn nevers() -> Empty<Never> {
123    empty()
124}