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

Implement try_from_bytes for Pattern #5034

Merged
merged 49 commits into from
Jun 14, 2024

Conversation

younies
Copy link
Member

@younies younies commented Jun 11, 2024

No description provided.

@younies younies requested review from sffc and zbraniecki June 11, 2024 13:12
utils/pattern/src/common.rs Outdated Show resolved Hide resolved
utils/pattern/src/multi_named.rs Outdated Show resolved Hide resolved
younies and others added 3 commits June 12, 2024 14:20
Co-authored-by: Shane F. Carr <shane@unicode.org>
Co-authored-by: Shane F. Carr <shane@unicode.org>
Comment on lines 233 to 244
pub fn try_from_utf8(bytes: &[u8]) -> Result<Self, Error> {
let store = B::try_store_from_utf8(bytes).map_err(|_| Error::InvalidPattern)?;
#[cfg(debug_assertions)]
match B::validate_store(core::borrow::Borrow::borrow(&store)) {
Ok(()) => (),
Err(e) => {
debug_assert!(false, "Pattern validation failed: {:?}", e);
}
};

Ok(Self::from_store_unchecked(store.to_owned()))
}
Copy link
Member

Choose a reason for hiding this comment

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

Issue: You should always call validate_store here, not just in debug_assertions

Copy link
Member Author

Choose a reason for hiding this comment

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

done, but why this is not the case in try_from_items ?

Copy link
Member

Choose a reason for hiding this comment

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

Because you are constructing from a store that you haven't validated yet

Copy link
Member Author

Choose a reason for hiding this comment

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

but try_store_from_utf8 has already validated it

@@ -101,6 +103,10 @@ pub trait PatternBackend: crate::private::Sealed + 'static {
where
Self::Store: ToOwned;

/// Converts a byte slice store to this pattern backend's store.
#[doc(hidden)] // TODO(#4467): Should be internal
fn try_store_from_utf8(utf8: &[u8]) -> Result<&Self::Store, Utf8Error>;
Copy link
Member

Choose a reason for hiding this comment

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

Issue: This should return a trait type, not always Utf8Error. You should add a new associated type, as I had in my suggestion from earlier.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, I added StoreUtf8Error

@younies younies marked this pull request as ready for review June 12, 2024 14:15
@younies younies requested a review from sffc June 12, 2024 14:15
@@ -270,6 +270,7 @@ impl PatternBackend for DoublePlaceholder {
#[cfg(feature = "alloc")]
type PlaceholderKeyCow<'a> = DoublePlaceholderKey;
type Error<'a> = Infallible;
type StoreUtf8Error = StoreUtf8Error;
Copy link
Member

Choose a reason for hiding this comment

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

Issue: it should return just Utf8Error

Copy link
Member Author

@younies younies Jun 12, 2024

Choose a reason for hiding this comment

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

There is no way to convert PatternError to Utf8Error or even to creat an instance of Utf8Error

Copy link
Member

Choose a reason for hiding this comment

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

try_store_from_utf8 should do exactly 1 thing, which is convert [u8] to the generic type Store. If you want it to also validate the pattern, then the function should be named validate_store_utf8 or similar. But I suggested try_store_from_utf8 since it is the minimal operation that we need.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@younies younies requested a review from sffc June 12, 2024 16:05
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

Objectively, you're calling validate_store twice, which is a bug. I suggested an approach to fix it.

utils/pattern/src/error.rs Outdated Show resolved Hide resolved
utils/pattern/src/single.rs Outdated Show resolved Hide resolved
utils/pattern/src/multi_named.rs Outdated Show resolved Hide resolved
utils/pattern/src/frontend/mod.rs Outdated Show resolved Hide resolved
utils/pattern/src/double.rs Outdated Show resolved Hide resolved
younies and others added 3 commits June 13, 2024 11:16
Co-authored-by: Shane F. Carr <shane@unicode.org>
Co-authored-by: Shane F. Carr <shane@unicode.org>
Co-authored-by: Shane F. Carr <shane@unicode.org>
utils/pattern/src/lib.rs Outdated Show resolved Hide resolved
younies and others added 3 commits June 13, 2024 17:16
Co-authored-by: Shane F. Carr <shane@unicode.org>
@younies younies requested a review from sffc June 13, 2024 15:49
sffc
sffc previously approved these changes Jun 13, 2024
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

The code is good! Thank you!

I'm having second thoughts on some names, but we can merge this and bikeshed names later.

{
/// Creates a pattern from a UTF-8 encoded byte slice.
///
/// ✨ *Enabled with the `alloc` Cargo feature.*
Copy link
Member

Choose a reason for hiding this comment

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

Nit: It does not need the alloc feature

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@younies younies requested a review from sffc June 13, 2024 18:09
@@ -2,27 +2,27 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

extern crate alloc;
Copy link
Member

Choose a reason for hiding this comment

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

Oops, you shouldn't need this. The bounds on your new impl are wrong. They should be on Store, not Store::Owned.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried as much as I can, could you fix it please

@younies younies requested a review from sffc June 13, 2024 21:11
@sffc
Copy link
Member

sffc commented Jun 14, 2024

OK I pushed 9180c26 which fixed the alloc issue

Then I pushed a few more commits to change the names. I realized that this isn't the right context to use the name utf8 since we don't guarantee that all patterns will forever have utf-8 backing stores.

@sffc sffc changed the title Implement try_from_utf8 for Pattern Implement try_from_bytes for Pattern Jun 14, 2024
@younies
Copy link
Member Author

younies commented Jun 14, 2024

OK I pushed 9180c26 which fixed the alloc issue

Then I pushed a few more commits to change the names. I realized that this isn't the right context to use the name utf8 since we don't guarantee that all patterns will forever have utf-8 backing stores.

Okay, thanks a lot

I have pushed small commit to fix the doc, it can be merged now

@younies younies merged commit 21efbae into unicode-org:main Jun 14, 2024
28 checks passed
@younies younies deleted the dev-pattern-try-from-u8 branch June 27, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants