tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.59k stars 2.45k forks source link

Include relevant values in ReadBuf.put_slice() panic message #6580

Closed OmegaJak closed 3 months ago

OmegaJak commented 4 months ago

Is your feature request related to a problem? Please describe. A service I work on panicked due to the assert at the top of io::ReadBuf::put_slice(), however, the panic message did not include the values that were compared, which would help with debugging the problem.

Describe the solution you'd like If calling put_slice() panics as a result the remaining space in the ReadBuf being less than the slice's length, it would be nice for the panic message to include the values of self.remaining() and buf.len(). This is the assert in question. I would propose updating it to something like

assert!(
    self.remaining() >= buf.len(),
    "buf.len() must fit in remaining(); buf.len() = {}, remaining() = {}",
    buf.len(),
    self.remaining()
);

Describe alternatives you've considered Additional logging of these values can be done by users of this function. I suppose it could also return a Result with an error that contains the mismatch but that's a much bigger change that probably has its own downsides.

Additional context There does seem to be a number of these asserts scattered around tokio and I couldn't find any that included the errant values in the message for a greater than/less than check. If including the values in the error message is a desirable behavior, then it may be worth thinking about adding/using assert macros like assert_eq! for the various greater than/less than comparisons (assert_ge!, assert_le!, etc).

Darksonn commented 4 months ago

This seems like a good idea.