Skip to main content

sark_http/field/
raw.rs

1use std::str;
2
3use crate::field::{Entry, Lower, Name, Value, metadata::map::record, storage};
4
5pub(in crate::field) fn name(name: Name<&[u8]>) -> &str {
6    // SAFETY: a Name admits only ASCII HTTP token bytes; its immutable borrow
7    // preserves that proof and therefore UTF-8 validity for this exact range.
8    unsafe { str::from_utf8_unchecked(name.get()) }
9}
10
11/// Restores admission retained by an immutable owner across packed storage.
12pub trait Admit<T>: Sized {
13    /// # Safety
14    /// The exact immutable input must retain every invariant of the target proof.
15    unsafe fn from_raw(owner: T) -> Self;
16}
17
18impl<T: storage::Bytes> Admit<T> for Name<T> {
19    /// # Safety
20    /// These exact immutable bytes must already prove a nonempty HTTP token.
21    unsafe fn from_raw(owner: T) -> Self {
22        Self(owner)
23    }
24}
25
26impl<T: storage::Bytes> Admit<T> for Lower<T> {
27    /// # Safety
28    /// These exact immutable bytes must already prove a nonempty lowercase HTTP token.
29    unsafe fn from_raw(owner: T) -> Self {
30        Self(owner)
31    }
32}
33
34impl<T: storage::Bytes> Admit<T> for Value<T> {
35    /// # Safety
36    /// These exact immutable bytes must satisfy `Error::is_text`; binary or layout admission is insufficient.
37    unsafe fn from_raw(owner: T) -> Self {
38        Self(owner)
39    }
40}
41
42impl<'a> Admit<record::Entry<'a>> for Entry<'a> {
43    /// # Safety
44    /// This immutable record must prove a lowercase token or supported pseudo-name and HTTP value bytes.
45    /// Packed geometry and binary metadata alone are insufficient.
46    unsafe fn from_raw(owner: record::Entry<'a>) -> Self {
47        Self(owner)
48    }
49}