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

Not validating tinystr keys #3406

Merged
merged 3 commits into from
May 4, 2023
Merged

Conversation

robertbastian
Copy link
Member

Fixes #2489

if deserializer.is_human_readable() {
Ok(TinyAsciiStr::deserialize(deserializer)?.raw())
} else {
Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Please manually invoke deserialize_bytes because otherwise this might use deserialize_seq which is probably not as efficient.

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 believe because they're fixed-size, arrays go through deserialize_tuple

use crate::TinyStrError;

#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub struct UnvalidatedTinyAsciiStr<const N: usize>(pub(crate) [u8; N]);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Public types need docs

Copy link
Member Author

Choose a reason for hiding this comment

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

TinyAsciiStr itself doesn't have docs...

Copy link
Member Author

Choose a reason for hiding this comment

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

Filed #3410

utils/tinystr/src/unvalidated.rs Show resolved Hide resolved
utils/tinystr/src/unvalidated.rs Outdated Show resolved Hide resolved
utils/tinystr/src/unvalidated.rs Outdated Show resolved Hide resolved
utils/tinystr/src/unvalidated.rs Show resolved Hide resolved
@robertbastian robertbastian requested review from sffc and removed request for dminor, zbraniecki, nciric and snktd May 3, 2023 19:04
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub struct UnvalidatedTinyAsciiStr<const N: usize>(pub(crate) [u8; N]);

impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
#[inline]
// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, TinyStrError> {
Copy link
Member

Choose a reason for hiding this comment

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

thought: could also be called try_into_validated()

(or just a TryInto impl?)

Copy link
Member Author

Choose a reason for hiding this comment

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

TryInto isn't as chainable, I added as_tinystr to the subtags in the PR for the same reason.

Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by chainable here? TryInto seems like the right trait for me and it abstracts better for generic inputs.

Copy link
Member

Choose a reason for hiding this comment

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

I think he's talking about how inference does not succeed if you do stuff like foo.try_into().bar()

Copy link
Member

Choose a reason for hiding this comment

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

We definitely should have TryInto, IMO it's acceptable to also have an inherent impl.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok, for any further readers, Robert meant this: TryInto::<Bar>::try_into(Foo {})?.baz(); being ugly.

Copy link
Member Author

Choose a reason for hiding this comment

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

UnvalidatedStr doesn't have a TryInto either

@robertbastian robertbastian merged commit b6c4018 into unicode-org:main May 4, 2023
@robertbastian robertbastian deleted the unvascii branch May 4, 2023 08:46
@Manishearth Manishearth mentioned this pull request Sep 21, 2023
13 tasks
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.

We should be using serde_bytes::Bytes instead of [u8]
4 participants