musli/context/
ignore.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use core::cell::Cell;
use core::fmt;
use core::marker::PhantomData;

use crate::alloc::{self, Allocator, String};
#[cfg(feature = "alloc")]
use crate::alloc::{System, SYSTEM};
#[cfg(test)]
use crate::mode::Binary;
use crate::Context;

use super::{ContextError, ErrorMarker};

/// A simple non-diagnostical capturing context which ignores the error and
/// loses all information about it (except that it happened).
pub struct Ignore<M, E, A> {
    alloc: A,
    error: Cell<bool>,
    _marker: PhantomData<(M, E)>,
}

#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl<M, E> Ignore<M, E, &'static System>
where
    E: ContextError,
{
    /// Construct a new ignoring context with a custom allocator.
    pub fn new() -> Self {
        Self::with_alloc(&SYSTEM)
    }
}

impl<M, E, A> Ignore<M, E, A> {
    /// Construct a new ignoring context.
    pub fn with_alloc(alloc: A) -> Self {
        Self {
            alloc,
            error: Cell::new(false),
            _marker: PhantomData,
        }
    }
}

#[cfg(test)]
impl<A> Ignore<Binary, ErrorMarker, A> {
    /// Construct a new ignoring context which collects an error marker.
    pub(crate) fn with_marker(alloc: A) -> Self {
        Self::with_alloc(alloc)
    }
}

impl<M, E, A> Ignore<M, E, A>
where
    E: ContextError,
{
    /// Construct an error or panic.
    pub fn unwrap(self) -> E {
        if self.error.get() {
            return E::message("error");
        }

        panic!("did not error")
    }
}

impl<M, E, A> Context for Ignore<M, E, A>
where
    M: 'static,
    A: Allocator,
{
    type Mode = M;
    type Error = ErrorMarker;
    type Mark = ();
    type Allocator = A;
    type String<'this> = String<'this, A> where Self: 'this;

    #[inline]
    fn clear(&self) {}

    #[inline]
    fn alloc(&self) -> &Self::Allocator {
        &self.alloc
    }

    #[inline]
    fn collect_string<T>(&self, value: &T) -> Result<Self::String<'_>, Self::Error>
    where
        T: ?Sized + fmt::Display,
    {
        alloc::collect_string(self, value)
    }

    #[inline]
    fn custom<T>(&self, _: T) -> ErrorMarker
    where
        T: 'static + Send + Sync + fmt::Display + fmt::Debug,
    {
        self.error.set(true);
        ErrorMarker
    }

    #[inline]
    fn message<T>(&self, _: T) -> ErrorMarker
    where
        T: fmt::Display,
    {
        self.error.set(true);
        ErrorMarker
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl<M, E> Default for Ignore<M, E, &'static System>
where
    E: ContextError,
{
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}