actix_http/header/
as_name.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
//! Sealed [`AsHeaderName`] trait and implementations.

use std::{borrow::Cow, str::FromStr as _};

use http::header::{HeaderName, InvalidHeaderName};

/// Sealed trait implemented for types that can be effectively borrowed as a [`HeaderValue`].
///
/// [`HeaderValue`]: super::HeaderValue
pub trait AsHeaderName: Sealed {}

pub struct Seal;

pub trait Sealed {
    fn try_as_name(&self, seal: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
}

impl Sealed for HeaderName {
    #[inline]
    fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
        Ok(Cow::Borrowed(self))
    }
}
impl AsHeaderName for HeaderName {}

impl Sealed for &HeaderName {
    #[inline]
    fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
        Ok(Cow::Borrowed(*self))
    }
}
impl AsHeaderName for &HeaderName {}

impl Sealed for &str {
    #[inline]
    fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
        HeaderName::from_str(self).map(Cow::Owned)
    }
}
impl AsHeaderName for &str {}

impl Sealed for String {
    #[inline]
    fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
        HeaderName::from_str(self).map(Cow::Owned)
    }
}
impl AsHeaderName for String {}

impl Sealed for &String {
    #[inline]
    fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
        HeaderName::from_str(self).map(Cow::Owned)
    }
}
impl AsHeaderName for &String {}