-
Notifications
You must be signed in to change notification settings - Fork 28
Relax filesizeformat
filter requirements
#216
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
Relax filesizeformat
filter requirements
#216
Conversation
5108afd
to
c7fce66
Compare
Finally fixed Ci \o/ |
rinja_derive/src/generator.rs
Outdated
buf.write(format_args!( | ||
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}(", | ||
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}(" | ||
)); | ||
self._visit_args(ctx, buf, args)?; | ||
buf.write(")?)"); |
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.
buf.write(format_args!( | |
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}(", | |
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}(" | |
)); | |
self._visit_args(ctx, buf, args)?; | |
buf.write(")?)"); | |
buf.write(format_args!( | |
"rinja::filters::HtmlSafeOutput(\ | |
rinja::filters::{name}(\ | |
rinja::helpers::get_primitive_value(&(" | |
)); | |
self._visit_args(ctx, buf, args)?; | |
buf.write( | |
"\ | |
)) as rinja::helpers::core::primitive::f32\ | |
)?\ | |
)" | |
); |
So filesizeformat()
does only need to accept f32.
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'm a bit shared about this. With the current approach, if users have something like a newtype, they can just implement the ToF32
trait on it to make it work. With your suggestion, they'd need to make a different kind of conversion before being able to call the filter. It sounds a bit less practical.
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 rather make rinja::helpers::PrimitiveType
public, so a user could implement something like that
impl PrimitiveType for DiskSpaceRequirements {
type Value = u64;
#[inline]
fn get(&self) -> u64 {
self.unpacked_size
}
}
Otherwise, if you want to add an explicit trait, I would rename it to something like AsFilesize
.
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.
Good idea! Let's go with PrimitiveType
as public trait. :)
rinja/src/filters/humansize.rs
Outdated
struct NbAndDecimal(usize); | ||
impl NbAndDecimal { | ||
fn new(nb: f32) -> Self { | ||
// `nb` will never be bigger than 999_999 so we're fine with usize. | ||
Self(nb as _) | ||
} | ||
} |
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 explicitly use NbAndDecimal(u32)
: usize
would be too small on 16bit systems, and on x64 sytems, working with u64
is a lot slower than u32
.
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 wondered about that but I guess you're right.
rinja/src/filters/humansize.rs
Outdated
|
||
impl fmt::Display for FilesizeFormatFilter { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_fmt(format_args!("{}", ISizeFormatter::new(self.0, &DECIMAL))) | ||
if self.0 < 1_000. { |
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 wonder if this whole if-else-if
block could be implemented as a loop: for (prefix, limit) in [("", 1e3), ("k", 1e6), ..] {
?
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.
It could, but the advantage of this approach is that we don't need to format the string of the units. :)
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 guess we could use [" B", " kB", ..]
including the space and 'B', and have fewer write calls.
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.
Now I'm not sure to follow what you mean. I have in mind:
for (unit, div) in [(" KB", 1e6), ...] {
if self.0 < div {
f.write_fmt(format_args!("{}{unit}", NbAndDecimal::new(self.0)))
}
}
That's still one extra argument formatting.
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.
write!(f, "{}", NbAndDecimal::new(self.0))?;
f.write_str(unit)?;
The extra call is there in the current implementation, too, it is just hidden in the unrolled code.
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.
Ok I see. Let's go with it then. Hopefully, loop will be unrolled at compile-time but it's not a hot path so should be fine.
a91846d
to
a2960ec
Compare
Remove `humansize` dependency
a2960ec
to
09466d8
Compare
Applied suggestions and fixed CI. :D |
I made some changes to the implementation, and pushed it to your PR. To test if my idea how to optimize |
Yup it's fine. I was going through the code and I discovered that I badly handled the case where the decimal ends with '0' and when the number is still too big (where we should not show decimals). Great improvements! :D |
c2086eb
to
2062ce4
Compare
@@ -39,7 +38,6 @@ with-warp = ["rinja_derive/with-warp"] | |||
[dependencies] | |||
rinja_derive = { version = "=0.3.5", path = "../rinja_derive" } | |||
|
|||
humansize = { version = "2", optional = true } |
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 removing a feature is a breaking change. Maybe it would be better to keep it as a noop for now?
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 can make the next release a major one. Problem solved like that. :)
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.
Works for me! :D
Thanks a lot! |
Fixes #215.
Funnily enough, it also allowed to remove one dependency, so yeay! \o/