Add convert::Into impls for HeaderName and HeaderValue

This commit is contained in:
Eliza Weisman
2017-07-25 12:12:12 -05:00
committed by Alex Crichton
parent 971fd01d3f
commit a90dae9780
2 changed files with 34 additions and 2 deletions
+26 -1
View File
@@ -1,7 +1,7 @@
use byte_str::ByteStr;
use bytes::{Bytes, BytesMut};
use std::{fmt, mem};
use std::{convert, fmt, mem};
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
@@ -32,6 +32,13 @@ pub struct HeaderName {
inner: Repr<Custom>,
}
impl convert::Into<Bytes> for HeaderName {
#[inline]
fn into(self) -> Bytes {
self.inner.into()
}
}
/// Almost a full `HeaderName`
#[derive(Debug, Hash)]
pub struct HdrName<'a> {
@@ -44,10 +51,28 @@ enum Repr<T> {
Custom(T),
}
impl<T> convert::Into<Bytes> for Repr<T>
where T: convert::Into<Bytes> {
fn into(self) -> Bytes {
match self {
Repr::Standard(header) =>
Bytes::from_static(header.as_str().as_bytes()),
Repr::Custom(header) => header.into()
}
}
}
// Used to hijack the Hash impl
#[derive(Debug, Clone, Eq, PartialEq)]
struct Custom(ByteStr);
impl convert::Into<Bytes> for Custom {
#[inline]
fn into(self) -> Bytes {
self.0.into()
}
}
#[derive(Debug, Clone)]
struct MaybeLower<'a> {
buf: &'a [u8],
+8 -1
View File
@@ -1,6 +1,6 @@
use bytes::Bytes;
use std::{char, cmp, fmt, str};
use std::{char, cmp, convert, fmt, str};
use std::error::Error;
use std::str::FromStr;
@@ -19,6 +19,13 @@ pub struct HeaderValue {
is_sensitive: bool,
}
impl convert::Into<Bytes> for HeaderValue {
#[inline]
fn into(self) -> Bytes {
self.inner
}
}
/// A possible error when converting a `HeaderValue` from a string or byte
/// slice.
#[derive(Debug)]