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

Rewrite <Duration as fmt::Display>::fmt() #25481

Closed
wants to merge 1 commit into from

Conversation

Projects
None yet
7 participants
@lilyball
Copy link
Contributor

lilyball commented May 16, 2015

Implement new formatting behavior that is smarter about the unit it uses
and handles precision.

format!("{}", Duration::new(0, 1_001)) // 1.001µs
format!("{}", Duration::new(0, 1_001_001)) // 1.001ms
format!("{:.6}", Duration::new(0, 1_001_001)) // 1.001001ms
// etc

The default is variable precision up to 3 decimals. No unit higher than
seconds are used.

See rust-lang/rfcs#1121 for more information.

Rewrite <Duration as fmt::Display>::fmt()
Implement new formatting behavior that is smarter about the unit it uses
and handles precision.

    format!("{}", Duration::new(0, 1_001)) // 1.001µs
    format!("{}", Duration::new(0, 1_001_001)) // 1.001ms
    format!("{:.6}", Duration::new(0, 1_001_001)) // 1.001001ms
    // etc

The default is variable precision up to 3 decimals. No unit higher than
seconds are used.

See rust-lang/rfcs#1121 for more information.
@lilyball

This comment has been minimized.

Copy link
Contributor Author

lilyball commented May 16, 2015

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented May 16, 2015

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

// precision in a way we don't want. At such time as it becomes reasonable to support
// padding without reimplementing fmt::Formatter::with_padding() here, this should be
// updated to support padding. In preparation for that, we're already writing to a
// stack buffer.

This comment has been minimized.

@alexcrichton

alexcrichton May 18, 2015

Member

Can you wrap this (and the remaining lines) to 100 characters? Although the style guide says 100 max it's more important to follow the style of the surrounding code.

This comment has been minimized.

@lilyball

lilyball May 22, 2015

Author Contributor

I'm not sure what you mean. You're asking me to wrap it to 100, which is already the limit it's wrapped at. Did you mean to ask for a different line length?

FWIW, I typically wrap doc comments at 79 but leave code at 100. I didn't wrap this to 79 because it's not a doc comment.

This comment has been minimized.

@alexcrichton

alexcrichton May 26, 2015

Member

Oh oops sorry I meant wrap to 80!

This comment has been minimized.

@lilyball

lilyball May 31, 2015

Author Contributor

I just tried wrapping to 80, and the resulting code is significantly harder to read, because a bunch of lines that fit comfortably with 100 now have to be wrapped awkwardly. The code here is indented more than the rest of the module, so they already don't have as much space.

I also don't understand why you think it's important to wrap this to 80 characters. You say it's important to follow the style of the surrounding code, but I don't see any justification at all for that other than the fact that you wrote the surrounding code and you personally prefer 80 characters. If someone else had written this file, I suspect you wouldn't think it's so important. In fact, I know you wouldn't, because prior to 556e76b this file was written for a line limit of 100.

In general, following the style of the surrounding code is a sensible policy when the project does not have a well-defined style. And even when it does, it still makes sense with small changes to preserve the cohesive style. But rust-lang/rust has a (reasonably-)well-defined style, and this is a significant chunk of new functionality (as opposed to individual lines here and there). And since the readability of this code suffers significantly with the shorter line length, I don't think it makes sense to restrict it like that.

debug_assert!(result.is_ok(), "Error in <Duration as Display>::fmt: {:?}", result);
let buf_str = try!(result.or(Err(fmt::Error)));
// If fmt::Formatter::with_padding() was public, this is where we'd use it.
write!(f, "{}", buf_str)

This comment has been minimized.

@alexcrichton

alexcrichton May 18, 2015

Member

This method is actually exposed as f.pad(buf_str), although another option here would also be to use buf_str.fmt(f)

This comment has been minimized.

@lilyball

lilyball May 22, 2015

Author Contributor

We actually can't use f.pad(buf_str), because it interprets precision as the maximum width of the string to print, but we're using precision here for a different purpose. I'm actually kind of surprised the formatting code doesn't have any way of changing the flags, and I decided not to attempt to hack it with a write!() call that uses a caller-supplied width because that discards alignment and fill.

// The following is >= the longest possible string we can format.
let mut buf = *b"18446744073709551615.999999999\xC2\xB5s";

fn write_to_buf<'a>(dur: &Duration, f: &fmt::Formatter, mut buf: &'a mut [u8])

This comment has been minimized.

@alexcrichton

alexcrichton May 18, 2015

Member

This seems quite complicated, and perhaps not getting a lot of bang for our buck, would it be possible to use something simpler which perhaps has an intermediate buffer allocation? It's quite unlikely that the fmt::Display implementation for Duration is going to be at the middle of a tight loop, and otherwise this code is somewhat difficult to follow as there's a lot going on.

This comment has been minimized.

@lilyball

lilyball May 22, 2015

Author Contributor

I don't think using a buffer allocation would simplify it, that would just end up writing to a Vec<u8> instead of a Cursor<&mut [u8]>, but the code otherwise would be the same. The complexity here comes just from the fact that I'm writing to an intermediate buffer at all, which I want to preserve because I think we should support padding/alignment on all stdlib fmt::Display implementations. The fmt API doesn't currently expose the necessary tools to do this, but if nobody else deals with that, I'm planning on at some point trying to figure out the right way to fix that so we can support padding properly.

Some of the complexity also comes from the fact that the flt2dec code is all private. I'd love to be able to explicitly call into that code with integral values for the various components, but for now I'm using the hack documented in this code. I figure I'll take a look again in the future when our new float formatting code is in place.

I do agree that this is a bit complicated. It ended up being more complicated than I had first expected. Part of that is due to the need to round the decimal when not printing at full precision (this rounding is the main reason I fell back to using the float serializing).

@lilyball

This comment has been minimized.

Copy link
Contributor Author

lilyball commented May 22, 2015

@alexcrichton Sorry for the delayed response, but I've now responded to your review comments.

@alexcrichton alexcrichton added the T-libs label May 26, 2015

@lilyball

This comment has been minimized.

Copy link
Contributor Author

lilyball commented Jul 26, 2015

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Jul 29, 2015

I think we may want to hold off on this until a decision on #26818 is reached, as that PR is deleting the display implementation.

@sfackler

This comment has been minimized.

Copy link
Member

sfackler commented Jul 30, 2015

It's only deleting the impl under the assumption that this wasn't good to go. If we think we're happy with this Display logic being used for all time, then I'll update my PR to not delete it.

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Aug 11, 2015

☔️ The latest upstream changes (presumably #26818) made this pull request unmergeable. Please resolve the merge conflicts.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Sep 27, 2015

So, with #26818 merged, what's the status of this PR?

@lilyball

This comment has been minimized.

Copy link
Contributor Author

lilyball commented Oct 19, 2015

Good question. I'd really like to see this get merged in. Obviously there's conflicts that need resolving now, but I haven't touched it as there has been no indication as to whether it will be accepted.

@alexcrichton alexcrichton removed the T-libs label Oct 19, 2015

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Oct 29, 2015

The libs team decided to close this during triage yesterday, more info can be found in my comment on #29146.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.