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

Writeable LengthHint should be called LengthBound #4709

Open
1 of 3 tasks
sffc opened this issue Mar 19, 2024 · 16 comments
Open
1 of 3 tasks

Writeable LengthHint should be called LengthBound #4709

sffc opened this issue Mar 19, 2024 · 16 comments
Labels
A-design Area: Architecture or design C-meta Component: Relating to ICU4X as a whole needs-approval One or more stakeholders need to approve proposal

Comments

@sffc
Copy link
Member

sffc commented Mar 19, 2024

We call it a hint, but it is stronger than a hint. The Writeable is required to conform to what it returns, or else it is not a valid Writeable. For example, if it returns a maximum length of 0, then it is valid to return an empty string without ever calling write_to.

OK?

@sffc sffc added C-meta Component: Relating to ICU4X as a whole A-design Area: Architecture or design needs-approval One or more stakeholders need to approve proposal labels Mar 19, 2024
@sffc
Copy link
Member Author

sffc commented Mar 19, 2024

Alternatively, we could just return an integer with the following invariant, which should cover all of our cases and maybe do it better:

Returns the 95th percentile upper bound of bytes required

This would permit a Writeable to estimate an upper bound while still permitting edge cases to exceed it.

@Manishearth
Copy link
Member

I think it should stay a hint, and we add that guidance yes.

@robertbastian
Copy link
Member

I think it should stay a hint, similar to iterator. Giving an incorrect hint will not produce incorrect or undefined behaviour, it's just a bit less efficient.

@sffc
Copy link
Member Author

sffc commented Mar 26, 2024

I think we should either:

  1. Make it a bound with minimum and maximum (as noted in the OP, we already have code that assumes the bounds are correct)
  2. Make it a hint and return an integer instead of a min/max

@Manishearth
Copy link
Member

Which code?

@sffc
Copy link
Member Author

sffc commented Mar 26, 2024

We have code that assumes it can skip calling write_to if write_len returns an upper bound of 0.

@Manishearth
Copy link
Member

yes, I am asking to see the code.

I think this discussion is not possible without being able to determine if that code really should be doing that and if that helps.

@Manishearth
Copy link
Member

Also if it's a zero thing we can still say something of the form that it's approximate but zero means zero

@sffc
Copy link
Member Author

sffc commented Mar 26, 2024

The code is in the writeable crate itself, but it is demonstrative of the type of assumption that other client code could want to be making

fn write_to_string(&self) -> Cow<str> {

    fn write_to_string(&self) -> Cow<str> {
        let hint = self.writeable_length_hint();
        if hint.is_zero() {
            return Cow::Borrowed("");
        }
        let mut output = String::with_capacity(hint.capacity());
        let _ = self.write_to(&mut output);
        Cow::Owned(output)
    }

@Manishearth
Copy link
Member

Okay, I think we have two paths:

  • We should declare that zero means zero, everything else is approximate, because that gives the most utility to users whilst leaving things flexible for implementors.
  • We treat it as a bound and name it as such.

@sffc
Copy link
Member Author

sffc commented Mar 29, 2024

I feel like my proposal to change the hint to an integer instead of an enum has gotten a bit lost in the shuffle.

@Manishearth
Copy link
Member

I think that could work, though I think in that case we still would want to be able to signal the difference between "empty" and "hint zero".

@zbraniecki
Copy link
Member

Option<usize> covers that.

@robertbastian
Copy link
Member

I think it is useful to be able to provide both upper and lower bounds, as this might very well affect the choice of sink that a client uses.

In #370 @sffc proposed an ::Approximate variant as well, but as of now I don't think we have any writeables that would want to return this.

@sffc
Copy link
Member Author

sffc commented Apr 12, 2024

@robertbastian What are your thoughts on "hint" vs "bound"?

@robertbastian
Copy link
Member

I think we should follow Iterator::size_hint, the same way we use std's fn len naming instead of fn length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-design Area: Architecture or design C-meta Component: Relating to ICU4X as a whole needs-approval One or more stakeholders need to approve proposal
Projects
None yet
Development

No branches or pull requests

4 participants