1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 18:02:39 +01:00
actix-web/actix-http/src/header/as_name.rs

52 lines
1.4 KiB
Rust
Raw Normal View History

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`].
///
2021-12-08 06:43:50 +01:00
/// [`HeaderValue`]: super::HeaderValue
2021-02-09 23:59:17 +01:00
pub trait AsHeaderName: Sealed {}
pub struct Seal;
2021-02-09 23:59:17 +01:00
pub trait Sealed {
fn try_as_name(&self, seal: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
2021-02-09 23:59:17 +01:00
}
impl Sealed for HeaderName {
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 {
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 {
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 {
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 {
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 {}