-
Notifications
You must be signed in to change notification settings - Fork 175
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
Conversation
if deserializer.is_human_readable() { | ||
Ok(TinyAsciiStr::deserialize(deserializer)?.raw()) | ||
} else { | ||
Ok(Self(<[u8; $size]>::deserialize(deserializer)?)) |
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: Please manually invoke deserialize_bytes
because otherwise this might use deserialize_seq
which is probably not as efficient.
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 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]); |
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: Public types need docs
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.
TinyAsciiStr
itself doesn't have docs...
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.
Filed #3410
#[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> { |
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.
thought: could also be called try_into_validated()
(or just a TryInto impl?)
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.
TryInto
isn't as chainable, I added as_tinystr
to the subtags in the PR for the same reason.
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.
What do you mean by chainable
here? TryInto
seems like the right trait for me and it abstracts better for generic inputs.
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 think he's talking about how inference does not succeed if you do stuff like foo.try_into().bar()
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.
We definitely should have TryInto
, IMO it's acceptable to also have an inherent impl.
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.
Ah, ok, for any further readers, Robert meant this: TryInto::<Bar>::try_into(Foo {})?.baz();
being ugly.
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.
UnvalidatedStr
doesn't have a TryInto
either
Fixes #2489