Skip to main content

intern_header_name

Function intern_header_name 

Source
pub fn intern_header_name(name: &str) -> &'static str
Expand description

Interns a header name so it can be attached to a runtime-materialized schema.

Schema::with_http_header is the one trait setter that requires &'static str rather than &'a str, because the HTTP binder hands header names to Headers::insert, which needs a Cow<'static, str>. Every other wire-format trait can borrow from storage the caller owns — an arena holding the parsed model text, for instance — and be dropped freely.

This function bridges that gap: it takes a runtime string and returns a &'static str suitable for Schema::with_http_header, keeping the binder’s zero-allocation fast path intact (HttpHeaderTrait::value_static returns Some for interned names).

§This leaks, deliberately

The returned &'static str is never freed. Interning is deduplicated, so a given name leaks at most once no matter how many times it is interned; the total is bounded by the number of distinct header names passed in, not by the number of calls. Re-materializing the same model repeatedly does not grow memory. For reference, the whole of Amazon S3 uses 158 distinct header names, about 4.4 KiB once.

Prefer this over a hand-rolled Box::leak, which leaks on every call and so grows without bound when placed in a loop.

§When this is the wrong tool

Because the memory is never reclaimed, the bound is “every distinct header name this process has ever seen”. That is fine for a client that loads its models at startup. It is not fine for a long-lived process that keeps ingesting new models, where the total grows without limit and cannot be reclaimed. Such callers should not intern; they need @httpHeader names borrowed from their own storage, which is an additive relaxation of Schema::with_http_header rather than something an interner can provide.

§Examples

use aws_smithy_schema::intern_header_name;

// A header name that only exists at runtime.
let parsed = String::from("x-amz-request-id");
let name: &'static str = intern_header_name(&parsed);
assert_eq!(name, "x-amz-request-id");

// Interning the same name again returns the identical pointer, not a copy.
assert!(std::ptr::eq(name, intern_header_name("x-amz-request-id")));