Skip to main content

static_table_entries

Function static_table_entries 

Source
pub const fn static_table_entries<S: FieldSection<'static>, const N: usize>(
    table: &[&'static str; N],
) -> [StaticTableEntry<S::Identity>; N]
Expand description

Classify each name in a protocol’s static table of lowercase field names, such as HPACK’s or QPACK’s, for section S, at compile time, so a decoder can interpret indexed fields without comparing names.

For a section with a lifetime, name it with 'static, such as Request<'static>; its entries serve the section with any lifetime.

use base_message::{Classification, FieldSection, FieldType, FieldValueError, StaticTableEntry, ascii_text, combination::Singleton, static_table_entries};

struct Host(String);

impl FieldType<'_> for Host {
    type Combination = Singleton;
    fn parse(value: &[u8]) -> Result<Self, FieldValueError> {
        Ok(Self(ascii_text(value)?.to_owned()))
    }
}

#[derive(FieldSection)]
#[fields(forbid("transfer-encoding"))]
struct Request {
    #[field("host")]
    host: Option<Host>,
}

const NAMES: [&str; 3] = ["host", "accept", "transfer-encoding"];
const TABLE: [StaticTableEntry<Request>; 3] = static_table_entries::<Request, 3>(&NAMES);
let classifications = TABLE.map(|entry| entry.classification());
assert_eq!(classifications, [Classification::Declaration(0), Classification::Undeclared, Classification::Forbidden]);

let request = Request::interpret([(TABLE[0], &b"example.com"[..])]).unwrap();
assert_eq!(request.host.map(|host| host.0).as_deref(), Some("example.com"));

§Panics

When a name in the table is not lowercase visible ASCII, such as "Transfer-Encoding", since a section’s names are lowercase and such a name would never match the declaration or forbidden name it spells; or when a name has a declaration id above 65,535. A constant reports either as a compile-time error.