Skip to main content

fast_down/
proxy.rs

1use std::ops::Deref;
2
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5pub enum Proxy<T> {
6    No,
7    #[default]
8    System,
9    Custom(T),
10}
11
12impl<T> Proxy<T> {
13    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Proxy<U> {
14        match self {
15            Self::No => Proxy::No,
16            Self::System => Proxy::System,
17            Self::Custom(t) => Proxy::Custom(f(t)),
18        }
19    }
20
21    pub fn as_deref(&self) -> Proxy<&T::Target>
22    where
23        T: Deref,
24    {
25        match self {
26            Self::No => Proxy::No,
27            Self::System => Proxy::System,
28            Self::Custom(t) => Proxy::Custom(&**t),
29        }
30    }
31
32    pub const fn as_ref(&self) -> Proxy<&T> {
33        match self {
34            Self::No => Proxy::No,
35            Self::System => Proxy::System,
36            Self::Custom(t) => Proxy::Custom(t),
37        }
38    }
39}