Skip to content
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

Move unsafe implementations to be in one place. #5104

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions utils/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,47 +191,6 @@ where
}
}

impl<B> Pattern<B, B::Store>
where
B: PatternBackend,
{
/// Creates a pattern from a borrowed store.
///
/// # Safety
///
/// `Pattern::validate_store(store)` must return `Ok`.
///
/// # Examples
///
/// ```
/// use core::str::FromStr;
/// use icu_pattern::Pattern;
/// use icu_pattern::SinglePlaceholder;
/// use writeable::assert_writeable_eq;
///
/// // Create a pattern from a valid string:
/// let allocated_pattern =
/// Pattern::<SinglePlaceholder, String>::from_str("{0} days")
/// .expect("valid pattern");
///
/// // Transform the store and create a new Pattern. This is valid because
/// // we call `.take_store()` and `.from_store_unchecked()` on patterns
/// // with the same backend (`SinglePlaceholder`).
/// let store = allocated_pattern.take_store();
///
/// // SAFETY: store comes from a valid pattern
/// let borrowed_pattern: &Pattern<SinglePlaceholder, _> =
/// unsafe { Pattern::from_borrowed_store_unchecked(store.as_str()) };
///
/// assert_writeable_eq!(borrowed_pattern.interpolate([5]), "5 days");
/// ```
pub unsafe fn from_borrowed_store_unchecked(store: &B::Store) -> &Self {
// SAFETY: The layout of `Pattern` is the same as `Store`
// because `_backend` is zero-sized and `store` is the only other field.
&*(store as *const B::Store as *const Self)
}
}

#[cfg(feature = "alloc")]
impl<B> Pattern<B, <B::Store as ToOwned>::Owned>
where
Expand Down
42 changes: 40 additions & 2 deletions utils/pattern/src/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ impl<'a> ZeroMapKV<'a> for Pattern<SinglePlaceholder, str> {
type OwnedType = Box<Pattern<SinglePlaceholder, str>>;
}

/// Implement `VarULE` for `Pattern<SinglePlaceholder, str>`.
///
/// # Safety
///
/// Safety checklist for `ULE`:
Expand All @@ -28,6 +30,39 @@ impl<'a> ZeroMapKV<'a> for Pattern<SinglePlaceholder, str> {
/// 5. The implementation of `from_byte_slice_unchecked()` returns a reference to the same data.
/// 6. `parse_byte_slice()` is equivalent to `validate_byte_slice()` followed by `from_byte_slice_unchecked()`.
/// 7. `str` byte equality is semantic equality.
///
/// # Examples
///
/// ```
/// use core::str::FromStr;
/// use icu_pattern::Pattern;
/// use icu_pattern::SinglePlaceholder;
/// use writeable::assert_writeable_eq;
/// use zerovec::ule::VarULE;
///
/// // Create a pattern from a valid string:
/// let allocated_pattern =
/// Pattern::<SinglePlaceholder, String>::from_str("{0} days")
/// .expect("valid pattern");
///
/// // Transform the store and create a new Pattern. This is valid because
/// // we call `.take_store()` and `.from_byte_slice_unchecked()` on patterns
/// // with the same backend (`SinglePlaceholder`).
/// let store = allocated_pattern.take_store();
///
/// assert!(
/// unsafe {
/// Pattern::<SinglePlaceholder, str>::validate_byte_slice(store.as_bytes()).is_ok()
/// });
///
/// // SAFETY: store comes from a valid pattern
/// let borrowed_pattern: &Pattern<SinglePlaceholder, _> =
/// unsafe {
/// Pattern::<SinglePlaceholder, str>::from_byte_slice_unchecked(store.as_bytes())
/// };
///
/// assert_writeable_eq!(borrowed_pattern.interpolate([5]), "5 days");
/// ```
unsafe impl VarULE for Pattern<SinglePlaceholder, str> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sffc , in my opinion, this should be

unsafe impl<B> VarULE for Pattern<B, B::Store>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems it needs more thinking, lets generalize it in another PR

fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
SinglePlaceholderPattern::try_from_utf8_store(bytes)
Expand All @@ -39,7 +74,10 @@ unsafe impl VarULE for Pattern<SinglePlaceholder, str> {
// SAFETY: As `validate_byte_slice` succeeded, `try_from_utf8_store` succeeded, which implies valid UTF-8
let store = core::str::from_utf8_unchecked(bytes);

// SAFETY: As `validate_byte_slice` succeeded, `try_from_utf8_store` also succeeded
SinglePlaceholderPattern::from_borrowed_store_unchecked(store)
// SAFETY:
// As `validate_byte_slice` succeeded, this means `store` is valid.
// And the layout of `Pattern` is the same as `Store`
// because `_backend` is zero-sized and `store` is the only other field.
&*(store as *const str as *const Self)
}
}
Loading