Skip to main content

WireDecode

Trait WireDecode 

Source
pub trait WireDecode<'headers, 'buf>: Sized {
    // Required method
    fn decode(
        buf: &'buf [u8],
        headers: &'headers mut [Header<'buf>],
    ) -> Result<(Self, usize), WireError>;

    // Provided method
    fn decode_uninit(
        buf: &'buf [u8],
        headers: &'headers mut [MaybeUninit<Header<'buf>>],
    ) -> Result<(Self, usize), WireError> { ... }
}
Expand description

Decode HTTP messages from raw bytes.

This trait provides two methods for decoding:

  • decode: Uses initialized headers storage (works for all types)
  • decode_uninit: Uses uninitialized headers storage (optimization, only available for some types)

§Examples

§Decoding a request (standard method)

use http_wire::WireDecode;
use http_wire::request::FullRequest;

let raw = b"GET /api/users HTTP/1.1\r\nHost: example.com\r\n\r\n";
let mut headers = [httparse::EMPTY_HEADER; 16];
let (request, total_len) = FullRequest::decode(raw, &mut headers).unwrap();

assert_eq!(request.head.method, Some("GET"));
assert_eq!(request.head.path, Some("/api/users"));

§Decoding a request (optimized method with uninitialized headers)

use http_wire::WireDecode;
use http_wire::request::FullRequest;
use std::mem::MaybeUninit;

let raw = b"GET /api/users HTTP/1.1\r\nHost: example.com\r\n\r\n";
let mut headers = [const { MaybeUninit::uninit() }; 16];
let (request, total_len) = FullRequest::decode_uninit(raw, &mut headers).unwrap();

assert_eq!(request.head.method, Some("GET"));

§Decoding a response

use http_wire::WireDecode;
use http_wire::response::FullResponse;

let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello";
let mut headers = [httparse::EMPTY_HEADER; 16];
let (response, total_len) = FullResponse::decode(raw, &mut headers).unwrap();

assert_eq!(response.head.code, Some(200));
assert_eq!(response.body, b"hello");

Note: decode_uninit is not available for FullResponse because the underlying httparse::Response parser does not support uninitialized headers.

Required Methods§

Source

fn decode( buf: &'buf [u8], headers: &'headers mut [Header<'buf>], ) -> Result<(Self, usize), WireError>

Decode using initialized headers storage.

This method works for both requests and responses.

§Arguments
  • buf - The buffer containing the raw HTTP message bytes
  • headers - A mutable slice of initialized Header structs to store parsed headers
§Returns

Returns Ok((Self, usize)) where Self is the decoded message and usize is the total length of the message in bytes (headers + body).

§Errors

Returns WireError if:

  • Headers are incomplete (WireError::PartialHead)
  • Body is incomplete (WireError::IncompleteBody)
  • Chunked encoding is malformed (WireError::InvalidChunkedBody)

Provided Methods§

Source

fn decode_uninit( buf: &'buf [u8], headers: &'headers mut [MaybeUninit<Header<'buf>>], ) -> Result<(Self, usize), WireError>

Decode using uninitialized headers storage (performance optimization).

This method avoids the overhead of initializing the headers array before parsing. It is only available for types where the underlying parser supports parse_with_uninit_headers. Currently only FullRequest implements this.

§Arguments
  • buf - The buffer containing the raw HTTP message bytes
  • headers - A mutable slice of uninitialized Header structs to store parsed headers
§Returns

Returns Ok((Self, usize)) where Self is the decoded message and usize is the total length of the message in bytes (headers + body).

§Errors

Returns the same errors as decode.

§Panics

The default implementation panics with an explanatory message for types that don’t support this optimization (e.g., FullResponse).

Examples found in repository?
examples/decode.rs (line 115)
107fn decode_request_optimized() {
108    let raw =
109        b"GET /api/data HTTP/1.1\r\nHost: api.example.com\r\nAccept: application/json\r\n\r\n";
110
111    // Use uninitialized headers for maximum performance
112    // This avoids the overhead of initializing the headers array
113    let mut headers = [const { MaybeUninit::uninit() }; 16];
114
115    match FullRequest::decode_uninit(raw, &mut headers) {
116        Ok((request, total_len)) => {
117            println!("✓ Successfully decoded with uninit headers (faster!)");
118            println!("  Method: {}", request.head.method.unwrap());
119            println!("  Path: {}", request.head.path.unwrap());
120            println!("  Headers count: {}", request.head.headers.len());
121            println!("  Total length: {} bytes", total_len);
122            println!();
123            println!("  Note: decode_uninit() is faster because it skips header");
124            println!("        initialization. Use this for performance-critical code.");
125        }
126        Err(e) => eprintln!("✗ Error: {:?}", e),
127    }
128}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'headers, 'buf> WireDecode<'headers, 'buf> for FullRequest<'headers, 'buf>

Source§

impl<'headers, 'buf> WireDecode<'headers, 'buf> for FullResponse<'headers, 'buf>