mirror of
https://github.com/hickory-dns/hickory-dns.git
synced 2026-04-25 11:15:54 +03:00
[GH-ISSUE #1612] BinEncoder should work with anything AsMut<[u8]> #712
Labels
No labels
blocked
breaking-change
bug
bug:critical
bug:tests
cleanup
compliance
compliance
compliance
crate:all
crate:client
crate:native-tls
crate:proto
crate:recursor
crate:resolver
crate:resolver
crate:rustls
crate:server
crate:util
dependencies
docs
duplicate
easy
easy
enhance
enhance
enhance
feature:dns-over-https
feature:dns-over-quic
feature:dns-over-tls
feature:dnsssec
feature:global_lb
feature:mdns
feature:tsig
features:edns
has workaround
ops
perf
platform:WASM
platform:android
platform:fuchsia
platform:linux
platform:macos
platform:windows
pull-request
question
test
tools
tools
trust
unclear
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/hickory-dns#712
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @zhuhaow on GitHub (Jan 5, 2022).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/1612
I want to dump the binary representation of the result of
Messageinto a buffer of raw IP packet.Working with current API requires us to use
BinEncoderwithpub fn with_offset(buf: &'a mut Vec<u8>, offset: u32, mode: EncodeMode)which would do the work but requires us to know the offset and own a ref toVecwhich may not be the case (e.g., when we are usingbytes).But if
BinEncodercan be generalized to takeT: AsMut<[u8]>everything will be significantly simpler, including the API ofBinEncoder.I've checked the code and I feel it's possible to do it. The only thing is the
into_bytes(self)method would be a little misleading as it's better calledinto_inner.Is there anything I'm missing that prevents us doing it?
I would love to make the PR.
@zhuhaow commented on GitHub (Jan 5, 2022):
One potential breaking change is that currently we are reserving space of the buffer for the caller, which is not possible with
AsMut.But error out when emitting is probably more reasonable in this case if there is no enough space. The caller should allocate proper space.
@djc commented on GitHub (Jan 5, 2022):
It seems reasonable to add some API that just takes
&mut [u8](and returns an error if there's not enough space). I don't think we'd want to get rid of theVec<u8>-based API though.@bluejekyll commented on GitHub (Jan 6, 2022):
I guess I don't have a problem with this, but I do have some concerns with essentially asking the caller to predict the space required. I'm wondering if we could do something with specialization on
Extend<u8>. It might mean a larger change though to extend with iterators (might be feasible). The compiler might get angry with two conflicting impls onT: AsMut<[u8]> + Extend<u8>and justT: AsMut<[u8]>.Actually, could we just have a specialization on
&'_ mut Vec<u8>andT: AsMut<u8>?@djc commented on GitHub (Jan 6, 2022):
Maybe just generalizing from
&mut Vec<u8>toExtend<u8>is actually enough, if the actual goal is just to pass in aBytesMut?@zhuhaow commented on GitHub (Jan 7, 2022):
In this case we may need to use
AsMut<[u8]> + Extend<u8>since we won't have length information with onlyExtend.Honestly I still find it a little surprising that the method will adjust the buffer size implicitly for us (both enlarge and shrink). Usually in this case when the buffer length will be changed, trait like
std::io::ReadBuforbytes::BufMutis used instead of specifing the real storage type directly so the behavior is implied. Of course, generally these traits expect we work with data with either known size or chuck of a stream with arbitrary size, which is not applicable in this case.@djc commented on GitHub (Jan 7, 2022):
ReadBuf is still unstable. BufMut seems like a reasonable option to me.
@zhuhaow commented on GitHub (Jan 7, 2022):
However, we cannot resize with
BufMut, which then requires us to ask the caller to provide a buffer that is large enough.@zhuhaow commented on GitHub (Jan 7, 2022):
I still think it makes sence to enable user to dump the result to a buffer with enough size and errors out when it doesn't.
This avoids a potential copy most of the time.
@djc commented on GitHub (Jan 7, 2022):
That might be reasonable for your use case, but other users might not care enough about the performance implications of the reallocation and prefer the ergonomics of something that's infallible.
@bluejekyll commented on GitHub (Jan 7, 2022):
I suppose it's possible for us to change the API to generally be used with a predefined buffer size. We use 2k as the limit for EDNS today. The real negative with making this fallible and requiring the caller to reallocate is that I think that would mean reencoding the entire Message, which would incur a performance penanlty. The only issue with that is that I know we have some Messages in the server that are encoded once and use some iterators for the records. That would need to be updated in order to make reencoding possible.
@daxpedda commented on GitHub (Oct 4, 2023):
In #1987 this issue has come up as well. The type we receive from
h3is aimpl Buf, so the requirement here is either takingimpl Buforimpl Read.