Skip to main content

sark_http/field/
raw.rs

1use std::str;
2
3use crate::field::{Entry, 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 Value<T> {
27    /// # Safety
28    /// These exact immutable bytes must satisfy `Error::is_text`; binary or layout admission is insufficient.
29    unsafe fn from_raw(owner: T) -> Self {
30        Self(owner)
31    }
32}
33
34impl<'a> Admit<record::Entry<'a>> for Entry<'a> {
35    /// # Safety
36    /// This immutable record must prove a lowercase token or supported pseudo-name and HTTP value bytes.
37    /// Packed geometry and binary metadata alone are insufficient.
38    unsafe fn from_raw(owner: record::Entry<'a>) -> Self {
39        Self::from_record(owner)
40    }
41}