-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add icu4x_shared with some helpers #5101
Draft
sffc
wants to merge
7
commits into
unicode-org:main
Choose a base branch
from
sffc:icu4x_shared
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e650f1
WIP icu4x_shared
sffc 343d12a
Make it work
sffc a1e174c
Add VarULE impls
sffc d005345
Add Deserialize impls
sffc 4b96a13
Use a private module
sffc e66be58
Add Serialize impl to the macro helper
sffc 2d83e69
Cleanup
sffc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
/// Trait asserting that a type is `repr(transparent)`. Used as a bound | ||
/// for functions that require this invariant. | ||
/// | ||
/// # Safety | ||
/// | ||
/// 1. This outer type *must* be `repr(transparent)` over `Inner` | ||
/// 2. `validate_inner` *must* return `false` if `Inner` does not uphold | ||
/// invariants enforced by this outer type. | ||
pub(crate) unsafe trait Transparent<Inner: ?Sized> { | ||
#[allow(dead_code)] | ||
fn validate_inner(inner: &Inner) -> bool; | ||
#[allow(dead_code)] | ||
fn as_inner(&self) -> &Inner; | ||
} | ||
|
||
/// Implements private helper functions for `repr(transparent)` types. | ||
#[allow(unused_macros)] | ||
macro_rules! impl_transparent_helpers { | ||
($outer:ident($inner:path)) => { | ||
impl $outer { | ||
/// Casts the inner type to the outer type. | ||
/// | ||
/// This function is safe, but it does not validate invariants | ||
/// that the outer type might enforce. It is made available as | ||
/// a private fn which can be called by another fn. | ||
#[allow(dead_code)] | ||
const fn cast_ref_unchecked(inner: &$inner) -> &$outer | ||
where | ||
$outer: Transparent<$inner>, | ||
{ | ||
// Safety: Outer is repr(transparent) over Inner | ||
// (enforced via trait safety invariant) | ||
unsafe { &*(inner as *const $inner as *const $outer) } | ||
} | ||
/// Casts the inner type to the outer type. | ||
/// | ||
/// This function is safe, but it does not validate invariants | ||
/// that the outer type might enforce. It is made available as | ||
/// a private fn which can be called by another fn. | ||
#[allow(dead_code)] | ||
const fn cast_box_unchecked( | ||
inner: alloc::boxed::Box<$inner>, | ||
) -> alloc::boxed::Box<$outer> | ||
where | ||
$outer: Transparent<$inner>, | ||
{ | ||
// Safety: Outer is repr(transparent) over Inner | ||
// (enforced via trait safety invariant) | ||
unsafe { core::mem::transmute(inner) } | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// Implements `VarULE` on a `repr(transparent)` type. | ||
#[allow(unused_macros)] | ||
macro_rules! impl_transparent_varule { | ||
($outer:ident($inner:path)) => { | ||
// Safety: | ||
// | ||
// 1. `repr(transparent)`, enforced by trait bound, implies no padding | ||
// 2. `repr(transparent)`, enforced by trait bound, implies alignment 1 | ||
// 3. Composition of `repr(transparent)` `validate_by_slice` with | ||
// `validate_inner` from the `Transparent` trait implies | ||
// valid bytes | ||
// 4. The `repr(transparent)` `validate_byte_slice` implies | ||
// that all bytes are covered | ||
// 5. Composition of `repr(transparent)` `from_byte_slice_unchecked` with | ||
// `cast_ref_unchecked` retains the same reference | ||
// 6. Other methods are left as default | ||
// 7. Equality enforced via `Eq` bound | ||
unsafe impl zerovec::ule::VarULE for $outer | ||
where | ||
$outer: Transparent<$inner> + Eq, | ||
{ | ||
#[inline] | ||
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ZeroVecError> { | ||
let inner = <$inner>::parse_byte_slice(bytes)?; | ||
Self::validate_inner(inner) | ||
.then_some(()) | ||
.ok_or(zerovec::ZeroVecError::parse::<Self>()) | ||
} | ||
#[inline] | ||
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self { | ||
let inner = <$inner>::from_byte_slice_unchecked(bytes); | ||
Self::cast_ref_unchecked(inner) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// Implements `serde::Deserialize` on a `repr(transparent)` type. | ||
#[allow(unused_macros)] | ||
macro_rules! impl_transparent_serde { | ||
($outer:ident($inner:path)) => { | ||
impl serde::Serialize for $outer { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::ser::Serializer, | ||
Comment on lines
+102
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This impl is not bound on |
||
{ | ||
self.as_inner().serialize(serializer) | ||
} | ||
} | ||
impl<'de, 'a> serde::Deserialize<'de> for &'a $outer | ||
where | ||
'de: 'a, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let inner = <&$inner>::deserialize(deserializer)?; | ||
if !$outer::validate_inner(&inner) { | ||
return Err(<D::Error as serde::de::Error>::custom(concat!( | ||
"Failed validation: ", | ||
stringify!($outer) | ||
))); | ||
} | ||
Ok($outer::cast_ref_unchecked(inner)) | ||
} | ||
} | ||
impl<'de> serde::Deserialize<'de> for Box<$outer> { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let inner = <alloc::boxed::Box<$inner>>::deserialize(deserializer)?; | ||
if !$outer::validate_inner(&inner) { | ||
return Err(<D::Error as serde::de::Error>::custom(concat!( | ||
"Failed validation: ", | ||
stringify!($outer) | ||
))); | ||
} | ||
Ok($outer::cast_box_unchecked(inner)) | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't
Transparent
live inzerovec
? There doesn't seem to be a use for it outside of an easierVarULE
implementation.