Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{Header, HeaderValue};

/// An external trait adding helper methods to types which implement [`Header`] trait.
pub trait HeaderExt: Header + self::sealed::Sealed {
/// Encode this [`Header`] to [`HeaderValue`].
fn encode_to_value(&self) -> HeaderValue;
}

impl<H> HeaderExt for H
where
H: Header + self::sealed::Sealed,
{
fn encode_to_value(&self) -> HeaderValue {
let mut container = Vec::with_capacity(1);
self.encode(&mut container);
container.remove(0)
}
}

mod sealed {
pub trait Sealed {}
impl<H: crate::Header> Sealed for H {}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn encode_header() {
let header_value = crate::AcceptRanges::bytes().encode_to_value();
assert_eq!(header_value, HeaderValue::from_static("bytes"));
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ pub use http::header::{HeaderName, HeaderValue};
#[macro_use]
mod util;
mod common;
mod ext;
mod map_ext;

pub use self::common::*;
pub use self::ext::HeaderExt;
pub use self::map_ext::HeaderMapExt;