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

Always respect DisplayHint::Ascii for byte slices #321

Merged
merged 1 commit into from
Jan 7, 2021

Conversation

Sh3Rm4n
Copy link
Contributor

@Sh3Rm4n Sh3Rm4n commented Dec 20, 2020

This is a naive implementation, which assumes, that the Uxx value
is always in u8 range. It does not consider the given format type {=$type}

Also, it assumes that the element args array only contains one element

Fixes #318

@Sh3Rm4n Sh3Rm4n force-pushed the ascii branch 2 times, most recently from a258daf to d34c8d9 Compare December 20, 2020 19:47
_ => None,
})
.collect::<Vec<u8>>();
format_bytes(&vals, hint, &mut buf)?
Copy link
Contributor

Choose a reason for hiding this comment

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

When formatting a slice of structs (or anything that isn't a u8) with {=[?]:a}, I'd expect the display hint to be ignored, but the code looks like it'll instead output nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

For example, you might want to format this struct with {=[?]:a} in order to display the name field as an ASCII string:

struct Data<'a> {
    name: &'a [u8],
    value: bool,
}

@Sh3Rm4n
Copy link
Contributor Author

Sh3Rm4n commented Dec 22, 2020

I've resolved the concerns in #321 (comment), but I'm still making assumptions, where I'm not sure if they hold true for every case:

  1. If any element in a slice has the "{=u8}" format, does that mean, that all elements have this format?
  2. Does "{=u8}" format ensure, that the values are also always in u8 range?
  3. Do FormatSlice elements for "{=u8}" always contain only one value, and when is this not true?

And I feel, that this code can be written a bit more elegant / readable. But it works for now :)

Copy link
Member

@japaric japaric left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. This looks good. I left a suggestion.

Regarding your questions / assumptions

If any element in a slice has the "{=u8}" format, does that mean, that all elements have this format?

Yes. The format string (field) can vary from element to element (of a format slice) but only if we are formatting a slice of enums. In that case, the format string is the variant and not the complete enum (e.g. Foo({:u8}) (variant) vs Foo({:u8})|Bar({:u16}) (enum)). The format string of a single variant cannot be {=u8}, however.

Does "{=u8}" format ensure, that the values are also always in u8 range?

Yes, because the macros do type checking: the value must have u8 type.

Do FormatSlice elements for "{=u8}" always contain only one value, and when is this not true?

Yes.

let vals = elements
.iter()
// Assumption: Arguments for u8 has always only 1 element
.filter_map(|e| match e.args[0] {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest using map instead of filter_map and panicking if arg is not a single Arg::Uxx. Because: if for some reason (i.e. a bug) the program reaches that state then the current logic will silently ignore the errors and print the wrong thing. With a panic the bug will be immediately exposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Converted both assumptions to panics, if they are not true.

This is a naive implementation, which assumes, that the `Uxx` value
is always in `u8` range. It does not consider the given format type `{=$type}`

Also, it assumes that the element args array only contains one element
@Sh3Rm4n
Copy link
Contributor Author

Sh3Rm4n commented Jan 6, 2021

Rebased and squashed. Ready for review :)

Copy link
Member

@japaric japaric left a comment

Choose a reason for hiding this comment

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

Great work! Thanks

@japaric
Copy link
Member

japaric commented Jan 7, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 7, 2021

Build succeeded:

@bors bors bot merged commit 5786287 into knurling-rs:main Jan 7, 2021
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.

Byte slices ignore display hints when they appear behind a Format impl
3 participants