Skip to main content

errlanes/
reshape.rs

1//! Type-level lane edits: `WithDenied<RepoFault>`.
2//!
3//! The aliases here take a lane profile (`lanes!(..)`), a [`Fault`], a
4//! [`Fail`], or a [`Carrier`], and enable or disable one lane on it. A
5//! carrier reshapes to its built-in (`C::Repr`), never to a new enum.
6//!
7//! ```
8//! use errlanes::{Fault, WithDenied, lanes};
9//!
10//! type MyFault = Fault<lanes!(Transient, Fatal)>;
11//!
12//! fn gate() -> Result<(), WithDenied<MyFault>> {
13//!     Err(Fault::Denied(errlanes::Denied::new()))
14//! }
15//!
16//! let _: Result<(), Fault<lanes!(Denied, Transient, Fatal)>> = gate();
17//! ```
18use crate::{Carrier, Fail, Fault, LaneProfile};
19
20/// One lane edit per associated type; see the module docs. Implemented for
21/// profiles, [`Fault`], [`Fail`], and every [`Carrier`].
22pub trait Reshape {
23    type WithDenied;
24    type WithoutDenied;
25    type WithTransient;
26    type WithoutTransient;
27    type WithFatal;
28}
29
30impl<L: LaneProfile> Reshape for Fault<L> {
31    type WithDenied = Fault<L::WithDenied>;
32    type WithoutDenied = Fault<L::WithoutDenied>;
33    type WithTransient = Fault<L::WithTransient>;
34    type WithoutTransient = Fault<L::WithoutTransient>;
35    type WithFatal = Fault<L::WithFatal>;
36}
37
38impl<D, L: LaneProfile> Reshape for Fail<D, L> {
39    type WithDenied = Fail<D, L::WithDenied>;
40    type WithoutDenied = Fail<D, L::WithoutDenied>;
41    type WithTransient = Fail<D, L::WithTransient>;
42    type WithoutTransient = Fail<D, L::WithoutTransient>;
43    type WithFatal = Fail<D, L::WithFatal>;
44}
45
46/// A carrier reshapes through its built-in.
47impl<C: Carrier> Reshape for C
48where
49    C::Repr: Reshape,
50{
51    type WithDenied = <C::Repr as Reshape>::WithDenied;
52    type WithoutDenied = <C::Repr as Reshape>::WithoutDenied;
53    type WithTransient = <C::Repr as Reshape>::WithTransient;
54    type WithoutTransient = <C::Repr as Reshape>::WithoutTransient;
55    type WithFatal = <C::Repr as Reshape>::WithFatal;
56}
57
58/// `E` with the denied lane enabled.
59pub type WithDenied<E> = <E as Reshape>::WithDenied;
60/// `E` with the denied lane disabled (what `narrow_denied` returns).
61pub type WithoutDenied<E> = <E as Reshape>::WithoutDenied;
62/// `E` with the transient lane enabled.
63pub type WithTransient<E> = <E as Reshape>::WithTransient;
64/// `E` with the transient lane disabled (what `narrow_transient` returns).
65pub type WithoutTransient<E> = <E as Reshape>::WithoutTransient;
66/// `E` with the fatal lane enabled.
67pub type WithFatal<E> = <E as Reshape>::WithFatal;