Skip to main content

HeaderValueExt

Trait HeaderValueExt 

Source
pub trait HeaderValueExt: Sealed {
    // Required method
    fn from_shared(
        src: impl Into<Bytes>,
    ) -> Result<HeaderValue, InvalidHeaderValue>;
}
Expand description

Construction of HeaderValue from shared byte buffers.

This extension enables zero-copy (when possible) creation of header values from types convertible to bytes::Bytes by delegating to HeaderValue::from_maybe_shared.

Required Methods§

Source

fn from_shared(src: impl Into<Bytes>) -> Result<HeaderValue, InvalidHeaderValue>

Creates a HeaderValue from a source convertible to Bytes.

The provided bytes are validated through HeaderValue::from_maybe_shared. This is zero-copy when the source is backed by a single contiguous slice of memory capacity.

§Errors

Returns InvalidHeaderValue if the byte sequence contains invalid header value bytes.

§Examples
use bytesbuf::BytesView;
use bytesbuf::mem::GlobalPool;
use http::HeaderValue;
use http_extensions::HeaderValueExt;

let view = BytesView::copied_from_slice(b"application/json", &GlobalPool::new());
let value = HeaderValue::from_shared(view.to_bytes()).unwrap();
assert_eq!(value, "application/json");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl HeaderValueExt for HeaderValue

Implementors§