Skip to content

Require `&mut _` output stream type for the write_bytes! macro

Simon Sapin requested to merge topic/default/mut into branch/default

Background

Because it’s a macro that can expand to multiple "write" calls, write_bytes! assigns the output stream parameter expression to an internal binding in order to avoid evaluating multiple times an expression that could have side-effects.

To avoid taking ownership of the stream away from the caller, before this change it took a &mut $expression borrow and assigned that. However when the expression is another local binding, that required the binding to be mut even if it already had a &mut _ type as is typically the case inside implementations of the DisplayBytes trait.

This caused signatures that look redundant, using the mut keyword twice: mut out: &mut dyn Write.

Change

write_bytes! now requires the output stream expression to already be a &mut _ borrow, and assigns that to an internal binding.

This is less flexible for callers (and therefore a breaking change) but matches how functions and methods work (auto-ref applies to recievers of method calls but not other arguments), and allows DisplayBytes impls to have the less redundant out: &mut dyn Write signature.

Merge request reports