-
-
Notifications
You must be signed in to change notification settings - Fork 177
Implement Debug&Display traits for CStr16 #114
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
Conversation
Signed-off-by: imtsuki <me@qjx.app>
Note that it is an explicit invariant of CStr16 that its inner string should not contain a NUL, except for the trailing one which your implementation should not print. So this case should not emerge in practice... |
src/data_types/strs.rs
Outdated
type Item = &'a Char16; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.pos >= self.inner.0.len() { |
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.
...except if your implementation has an off-by-one, which I suspect it has. The last printable character of a CStr16 is the one at index len-2, but your loop will go up to the trailing NUL at index len-1.
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.
In this case, what I'm thinking about is that an Iterator
should not interpret its inner data too much and it may be sad for someone trying to manually iterate over a CStr16
and do some low-level operations but the iterator just simply skips the trailing NUL, so I pushed this check into the actual formatting process. Maybe this is not so good, however, because as far as I know std::string
in C++11 ends with \0
but its iterator does not return \0
. I can't tell which one is better.
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 would personally propose not to expose the trailing NUL, for three reasons:
- It is redundant because the Iterator API already has None as a terminator.
- As you observed in this PR, emitting it means that many clients will need to account for its presence and handle it specially, because it is not like the other
Char16
s in the stream (owing to being in-band metadata rather than actual data). - From the reverse perspective, I cannot think of any situation beyond simple
memcpy
where exposing it as part of a read-only string access primitive would eliminate work:- Taking substrings still generally requires adding extra NULs.
- Concatenating strings still requires removing intermediary NULs.
- ...and
memcpy
itself can probably be handled via lower-level access than thisIterator
, such as the one provided by theto_u16_slice_*
methods.
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 pushed a new commit and now CStr16
skips the trailing NUL.
Also, it's a bit sad that we need to do all this back-and-forth through |
Given that |
Signed-off-by: imtsuki <me@qjx.app>
Thanks for this change! I'll merge this as is then. @GabrielMajeri I manually checked the CI log and the failure is not related to this change, but what's in there might be helpful in your ongoing investigation of CI problems. Notice this bit at the end:
It seems the test suite does not run through to the end, otherwise you'd see a "shutting down" message. Instead, something bad is going on, most likely in this big MP protocol test. It would be a good idea to add some verbose logging to this test in order to figure out where exactly it's crashing, and hopefully why. |
This is my attempt to implement
Debug
andDisplay
traits for theCStr16
type using the traits that have already been implemented forChar16
.Strangely if I try to format
\u{0}
,Display
panics whileDebug
does not, but I don't see any essential differences between the implementations ofChar16
forDebug
andDisplay
. It seems that the following code is not working forimpl Display for Char16
:uefi-rs/src/data_types/chars.rs
Lines 121 to 123 in ba8cb63
This is the panic output:
So for now, the formatting process just ends before the null terminator.
Closes #98
Signed-off-by: imtsuki me@qjx.app