From 837b550238aa671a591ccf282dddeab29cadb206 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:42 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/unicode-bidi/src/level.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'vendor/unicode-bidi/src/level.rs') diff --git a/vendor/unicode-bidi/src/level.rs b/vendor/unicode-bidi/src/level.rs index f2e0d99ab..ef4f6d9e4 100644 --- a/vendor/unicode-bidi/src/level.rs +++ b/vendor/unicode-bidi/src/level.rs @@ -16,6 +16,7 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use core::convert::{From, Into}; +use core::slice; use super::char_data::BidiClass; @@ -31,6 +32,7 @@ use super::char_data::BidiClass; /// #[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[repr(transparent)] pub struct Level(u8); pub const LTR_LEVEL: Level = Level(0); @@ -194,6 +196,19 @@ impl Level { pub fn vec(v: &[u8]) -> Vec { v.iter().map(|&x| x.into()).collect() } + + /// Converts a byte slice to a slice of Levels + /// + /// Does _not_ check if each level is within bounds (`<=` [`MAX_IMPLICIT_DEPTH`]), + /// which is not a requirement for safety but is a requirement for correctness of the algorithm. + pub fn from_slice_unchecked(v: &[u8]) -> &[Level] { + debug_assert_eq!(core::mem::size_of::(), core::mem::size_of::()); + unsafe { + // Safety: The two arrays are the same size and layout-compatible since + // Level is `repr(transparent)` over `u8` + slice::from_raw_parts(v as *const [u8] as *const u8 as *const Level, v.len()) + } + } } /// If levels has any RTL (odd) level -- cgit v1.2.3