/// Equivalent to [`try_from_bytes(bytes[start..end])`](Self::try_from_bytes), /// but callable in a `const` context (which range indexing is not). pubconstfn try_from_bytes_manual_slice(
v: &[u8],
start: usize,
end: usize,
) -> Result<Self, crate::parser::errors::ParserError> { let slen = end - start;
#[allow(clippy::double_comparisons)] // if len_start == len_end if slen < $len_start || slen > $len_end { return Err(crate::parser::errors::ParserError::$error);
}
match tinystr::TinyAsciiStr::from_bytes_manual_slice(v, start, end) {
Ok($tinystr_ident) if $validate => Ok(Self($normalize)),
_ => Err(crate::parser::errors::ParserError::$error),
}
}
#[doc = concat!("Safely creates a [`", stringify!($name), "`] from its raw format")] /// as returned by [`Self::into_raw`]. Unlike [`Self::try_from_bytes`], /// this constructor only takes normalized values. pubconstfn try_from_raw(
v: [u8; $len_end],
) -> Result<Self, crate::parser::errors::ParserError> { iflet Ok($tinystr_ident) = tinystr::TinyAsciiStr::<$len_end>::try_from_raw(v) { if $tinystr_ident.len() >= $len_start && $is_normalized {
Ok(Self($tinystr_ident))
} else {
Err(crate::parser::errors::ParserError::$error)
}
} else {
Err(crate::parser::errors::ParserError::$error)
}
}
#[doc = concat!("Unsafely creates a [`", stringify!($name), "`] from its raw format")] /// as returned by [`Self::into_raw`]. Unlike [`Self::try_from_bytes`], /// this constructor only takes normalized values. /// /// # Safety /// /// This function is safe iff [`Self::try_from_raw`] returns an `Ok`. This is the case /// for inputs that are correctly normalized. pubconstunsafefn from_raw_unchecked(v: [u8; $len_end]) -> Self { Self(tinystr::TinyAsciiStr::from_bytes_unchecked(v))
}
/// Deconstructs into a raw format to be consumed by /// [`from_raw_unchecked`](Self::from_raw_unchecked()) or /// [`try_from_raw`](Self::try_from_raw()). pubconstfn into_raw(self) -> [u8; $len_end] {
*self.0.all_bytes()
}
#[inline] /// A helper function for displaying as a `&str`. pubconstfn as_str(&self) -> &str { self.0.as_str()
}
/// Compare with BCP-47 bytes. /// /// The return value is equivalent to what would happen if you first converted /// `self` to a BCP-47 string and then performed a byte comparison. /// /// This function is case-sensitive and results in a *total order*, so it is appropriate for /// binary search. The only argument producing [`Ordering::Equal`](core::cmp::Ordering::Equal) /// is `self.as_str().as_bytes()`. #[inline] pubfn strict_cmp(self, other: &[u8]) -> core::cmp::Ordering { self.as_str().as_bytes().cmp(other)
}
/// Compare with a potentially unnormalized BCP-47 string. /// /// The return value is equivalent to what would happen if you first parsed the /// BCP-47 string and then performed a structural comparison. /// #[inline] pubfn normalizing_eq(self, other: &str) -> bool { self.as_str().eq_ignore_ascii_case(other)
}
}
impl core::str::FromStr for $name { type Err = crate::parser::errors::ParserError;
// Safety checklist for ULE: // // 1. Must not include any uninitialized or padding bytes (true since transparent over a ULE). // 2. Must have an alignment of 1 byte (true since transparent over a ULE). // 3. ULE::validate_byte_slice() checks that the given byte slice represents a valid slice. // 4. ULE::validate_byte_slice() checks that the given byte slice has a valid length. // 5. All other methods must be left with their default impl. // 6. Byte equality is semantic equality. #[cfg(feature = "zerovec")] unsafeimpl zerovec::ule::ULE for $name { fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ZeroVecError> { let it = bytes.chunks_exact(core::mem::size_of::<Self>()); if !it.remainder().is_empty() { return Err(zerovec::ZeroVecError::length::<Self>(bytes.len()));
} for v in it { // The following can be removed once `array_chunks` is stabilized. letmut a = [0; core::mem::size_of::<Self>()];
a.copy_from_slice(v); ifSelf::try_from_raw(a).is_err() { return Err(zerovec::ZeroVecError::parse::<Self>());
}
}
Ok(())
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.