fs: avoid contents
copy for Vec<u8>
, String
, Bytes
write
#7199
+89
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
As discovered by #7183,
tokio::fs::write
always copies the whole buffer, even when it's not necessary (the buffer isSend + 'static
).Solution
This issue could have been fixed if we had proper specialization, but without it and with references involved we can't use
Any
from std directly. However, David made thetypeid
crate for comparingTypeId
s of non-'static
types. Thebincode
folks made their own crate,unty
crate, based on this same idea. This gives us poor's man specialization, allowing us to avoid the copy forVec<u8>
,String
andBytes
.Box[<u8>]
,Arc<[u8]>
and many otherSend + 'static
types could also be added in the future. Yesterday Armin also made a crate for doing something similar to this PR https://lucumr.pocoo.org/2025/3/23/from-string/.Compiler explorer shows good enough output and correct proper functioning https://rust.godbolt.org/z/ef5GaooxW.
Fixes: #7183