-
Notifications
You must be signed in to change notification settings - Fork 184
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
Conversation
Co-authored-by: Shane F. Carr <shane@unicode.org>
Co-authored-by: Shane F. Carr <shane@unicode.org>
utils/pattern/src/frontend/mod.rs
Outdated
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())) | ||
} |
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.
Issue: You should always call validate_store here, not just in debug_assertions
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.
done, but why this is not the case in try_from_items
?
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.
Because you are constructing from a store that you haven't validated yet
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.
but try_store_from_utf8
has already validated it
utils/pattern/src/common.rs
Outdated
@@ -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>; |
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.
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.
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.
yes, I added StoreUtf8Error
utils/pattern/src/double.rs
Outdated
@@ -270,6 +270,7 @@ impl PatternBackend for DoublePlaceholder { | |||
#[cfg(feature = "alloc")] | |||
type PlaceholderKeyCow<'a> = DoublePlaceholderKey; | |||
type Error<'a> = Infallible; | |||
type StoreUtf8Error = StoreUtf8Error; |
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.
Issue: it should return just Utf8Error
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.
There is no way to convert PatternError
to Utf8Error
or even to creat an instance of Utf8Error
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.
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.
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.
done
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.
Objectively, you're calling validate_store
twice, which is a bug. I suggested an approach to fix it.
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>
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.
The code is good! Thank you!
I'm having second thoughts on some names, but we can merge this and bikeshed names later.
utils/pattern/src/frontend/mod.rs
Outdated
{ | ||
/// Creates a pattern from a UTF-8 encoded byte slice. | ||
/// | ||
/// ✨ *Enabled with the `alloc` Cargo feature.* |
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.
Nit: It does not need the alloc
feature
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.
done
utils/pattern/src/frontend/mod.rs
Outdated
@@ -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; |
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.
Oops, you shouldn't need this. The bounds on your new impl are wrong. They should be on Store, not Store::Owned.
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.
I tried as much as I can, could you fix it please
…to dev-pattern-try-from-u8
OK I pushed 9180c26 which fixed the Then I pushed a few more commits to change the names. I realized that this isn't the right context to use the name |
try_from_utf8
for Patterntry_from_bytes
for Pattern
Okay, thanks a lot I have pushed small commit to fix the doc, it can be merged now |
No description provided.