Skip to content

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

Merged

Conversation

GuillaumeGomez
Copy link
Collaborator

Fixes #215.

Funnily enough, it also allowed to remove one dependency, so yeay! \o/

@GuillaumeGomez GuillaumeGomez force-pushed the improve-filesizeformat branch 3 times, most recently from 5108afd to c7fce66 Compare October 31, 2024 21:26
@GuillaumeGomez
Copy link
Collaborator Author

Finally fixed Ci \o/

Comment on lines 1663 to 1667
buf.write(format_args!(
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}(",
"rinja::filters::HtmlSafeOutput(rinja::filters::{name}("
));
self._visit_args(ctx, buf, args)?;
buf.write(")?)");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Collaborator Author

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.

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 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.

Copy link
Collaborator Author

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. :)

Comment on lines 71 to 35
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 _)
}
}
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 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.

Copy link
Collaborator Author

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.


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. {
Copy link
Member

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), ..] {?

Copy link
Collaborator Author

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. :)

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

@GuillaumeGomez GuillaumeGomez force-pushed the improve-filesizeformat branch 4 times, most recently from a91846d to a2960ec Compare October 31, 2024 23:50
@GuillaumeGomez
Copy link
Collaborator Author

Applied suggestions and fixed CI. :D

@Kijewski
Copy link
Member

Kijewski commented Nov 1, 2024

I made some changes to the implementation, and pushed it to your PR.

To test if my idea how to optimize NbAndDecimal could work, I kinda had to implement it. I hope that's OK! :-/

@GuillaumeGomez
Copy link
Collaborator Author

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

@Kijewski Kijewski force-pushed the improve-filesizeformat branch from c2086eb to 2062ce4 Compare November 1, 2024 21:51
@@ -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 }
Copy link
Member

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?

Copy link
Collaborator Author

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. :)

Copy link
Member

Choose a reason for hiding this comment

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

Works for me! :D

@GuillaumeGomez
Copy link
Collaborator Author

Thanks a lot!

@GuillaumeGomez GuillaumeGomez merged commit a9bf11b into askama-rs:master Nov 1, 2024
34 checks passed
@GuillaumeGomez GuillaumeGomez deleted the improve-filesizeformat branch November 1, 2024 22:06
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.

Allow filesizeformat to also work on &i64 (and any integers between any level of reference)
2 participants