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

Mock::with_chunked_body could use better documentation #190

Closed
mstange opened this issue Feb 9, 2024 · 5 comments
Closed

Mock::with_chunked_body could use better documentation #190

mstange opened this issue Feb 9, 2024 · 5 comments

Comments

@mstange
Copy link

mstange commented Feb 9, 2024

Reading the documentation for with_chunked_body I wasn't immediately sure how the callback function is used. Is it called once per chunk? Or just once? What determines the size of the chunks?

I think the answers are: It's called just once. Every call to write* on the writer argument becomes one chunk. The callback function can sleep between the write calls in order to put delays between the chunks. The callback function can also return an error after any number of write calls in order to abort the response.

Should the type of the callback function be FnOnce rather than Fn to emphasize the fact that it's only called once?

@lipanski
Copy link
Owner

lipanski commented Feb 11, 2024

That is correct and, as you pointed out, you can sleep between writes to simulate delay between the chunks. I'll make a note to update the docs but PRs are also welcome 😉

@lipanski
Copy link
Owner

Addressed in #191

@mstange
Copy link
Author

mstange commented Feb 21, 2024

Thank you! One question still remains: Should it be FnOnce instead?

@lipanski
Copy link
Owner

Aside from semantics do you see any other benefits in using FnOnce? I'm hesitant to change this since it would be a slight breaking change.

@mstange
Copy link
Author

mstange commented Feb 21, 2024

It would allow users to consume values in the callback that get moved in from outside the callback. For example, you might have a Vec that you move into the callback and then consume it by calling into_iter(). This would not compile with the Fn bound.
There are probably more compelling examples but I couldn't think of any :)

It's also not a problem I have myself. I agree that it's probably not worth doing a breaking change just for this, but maybe something to look at when making other breaking changes.

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

No branches or pull requests

2 participants