mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
add Seal argument to sealed AsHeaderName methods (#2252)
This commit is contained in:
parent
b1e841f168
commit
2e1d761854
@ -8,40 +8,42 @@ use http::header::{HeaderName, InvalidHeaderName};
|
|||||||
|
|
||||||
pub trait AsHeaderName: Sealed {}
|
pub trait AsHeaderName: Sealed {}
|
||||||
|
|
||||||
|
pub struct Seal;
|
||||||
|
|
||||||
pub trait Sealed {
|
pub trait Sealed {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
|
fn try_as_name(&self, seal: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sealed for HeaderName {
|
impl Sealed for HeaderName {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
Ok(Cow::Borrowed(self))
|
Ok(Cow::Borrowed(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for HeaderName {}
|
impl AsHeaderName for HeaderName {}
|
||||||
|
|
||||||
impl Sealed for &HeaderName {
|
impl Sealed for &HeaderName {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
Ok(Cow::Borrowed(*self))
|
Ok(Cow::Borrowed(*self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for &HeaderName {}
|
impl AsHeaderName for &HeaderName {}
|
||||||
|
|
||||||
impl Sealed for &str {
|
impl Sealed for &str {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for &str {}
|
impl AsHeaderName for &str {}
|
||||||
|
|
||||||
impl Sealed for String {
|
impl Sealed for String {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for String {}
|
impl AsHeaderName for String {}
|
||||||
|
|
||||||
impl Sealed for &String {
|
impl Sealed for &String {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -213,7 +213,7 @@ impl HeaderMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_value(&self, key: impl AsHeaderName) -> Option<&Value> {
|
fn get_value(&self, key: impl AsHeaderName) -> Option<&Value> {
|
||||||
match key.try_as_name().ok()? {
|
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||||
Cow::Borrowed(name) => self.inner.get(name),
|
Cow::Borrowed(name) => self.inner.get(name),
|
||||||
Cow::Owned(name) => self.inner.get(&name),
|
Cow::Owned(name) => self.inner.get(&name),
|
||||||
}
|
}
|
||||||
@ -279,7 +279,7 @@ impl HeaderMap {
|
|||||||
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
||||||
match key.try_as_name().ok()? {
|
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||||
Cow::Borrowed(name) => self.inner.get_mut(name).map(|v| v.first_mut()),
|
Cow::Borrowed(name) => self.inner.get_mut(name).map(|v| v.first_mut()),
|
||||||
Cow::Owned(name) => self.inner.get_mut(&name).map(|v| v.first_mut()),
|
Cow::Owned(name) => self.inner.get_mut(&name).map(|v| v.first_mut()),
|
||||||
}
|
}
|
||||||
@ -327,7 +327,7 @@ impl HeaderMap {
|
|||||||
/// assert!(map.contains_key(header::ACCEPT));
|
/// assert!(map.contains_key(header::ACCEPT));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn contains_key(&self, key: impl AsHeaderName) -> bool {
|
pub fn contains_key(&self, key: impl AsHeaderName) -> bool {
|
||||||
match key.try_as_name() {
|
match key.try_as_name(super::as_name::Seal) {
|
||||||
Ok(Cow::Borrowed(name)) => self.inner.contains_key(name),
|
Ok(Cow::Borrowed(name)) => self.inner.contains_key(name),
|
||||||
Ok(Cow::Owned(name)) => self.inner.contains_key(&name),
|
Ok(Cow::Owned(name)) => self.inner.contains_key(&name),
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
@ -410,7 +410,7 @@ impl HeaderMap {
|
|||||||
///
|
///
|
||||||
/// assert!(map.is_empty());
|
/// assert!(map.is_empty());
|
||||||
pub fn remove(&mut self, key: impl AsHeaderName) -> Removed {
|
pub fn remove(&mut self, key: impl AsHeaderName) -> Removed {
|
||||||
let value = match key.try_as_name() {
|
let value = match key.try_as_name(super::as_name::Seal) {
|
||||||
Ok(Cow::Borrowed(name)) => self.inner.remove(name),
|
Ok(Cow::Borrowed(name)) => self.inner.remove(name),
|
||||||
Ok(Cow::Owned(name)) => self.inner.remove(&name),
|
Ok(Cow::Owned(name)) => self.inner.remove(&name),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
|
Loading…
Reference in New Issue
Block a user