2021-11-29 03:22:47 +01:00
|
|
|
//! Sealed [`AsHeaderName`] trait and implementations.
|
2021-02-09 23:59:17 +01:00
|
|
|
|
2021-11-29 03:22:47 +01:00
|
|
|
use std::{borrow::Cow, str::FromStr as _};
|
2021-02-09 23:59:17 +01:00
|
|
|
|
|
|
|
use http::header::{HeaderName, InvalidHeaderName};
|
|
|
|
|
2021-11-29 03:22:47 +01:00
|
|
|
/// Sealed trait implemented for types that can be effectively borrowed as a [`HeaderValue`].
|
|
|
|
///
|
|
|
|
/// [`HeaderValue`]: crate::http::HeaderValue
|
2021-02-09 23:59:17 +01:00
|
|
|
pub trait AsHeaderName: Sealed {}
|
|
|
|
|
2021-06-08 13:57:19 +02:00
|
|
|
pub struct Seal;
|
|
|
|
|
2021-02-09 23:59:17 +01:00
|
|
|
pub trait Sealed {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, seal: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
|
2021-02-09 23:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Sealed for HeaderName {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
2021-02-09 23:59:17 +01:00
|
|
|
Ok(Cow::Borrowed(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsHeaderName for HeaderName {}
|
|
|
|
|
|
|
|
impl Sealed for &HeaderName {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
2021-02-09 23:59:17 +01:00
|
|
|
Ok(Cow::Borrowed(*self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsHeaderName for &HeaderName {}
|
|
|
|
|
|
|
|
impl Sealed for &str {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
2021-02-09 23:59:17 +01:00
|
|
|
HeaderName::from_str(self).map(Cow::Owned)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsHeaderName for &str {}
|
|
|
|
|
|
|
|
impl Sealed for String {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
2021-02-09 23:59:17 +01:00
|
|
|
HeaderName::from_str(self).map(Cow::Owned)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsHeaderName for String {}
|
|
|
|
|
|
|
|
impl Sealed for &String {
|
2021-06-08 13:57:19 +02:00
|
|
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
2021-02-09 23:59:17 +01:00
|
|
|
HeaderName::from_str(self).map(Cow::Owned)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsHeaderName for &String {}
|