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

add OctetsFrom implementations for common octet types #126

Closed
wants to merge 1 commit into from

Conversation

vavrusa
Copy link
Contributor

@vavrusa vavrusa commented Apr 13, 2022

Hi! 👋 This implements OctetsFrom trait for most common octet type conversions instead of providing a blanket implementation as a followup for #89

@vavrusa vavrusa force-pushed the upstream-pr branch 3 times, most recently from aaf1c99 to 327a188 Compare April 13, 2022 20:40
@partim
Copy link
Member

partim commented Apr 21, 2022

Thanks for the PR!

Looks like we are having MSRV issues in dependencies again. Could you merge main so the checks go green, both here and in #127?

This implements OctetsFrom trait for most common octet type conversions
instead of providing a blanket implementation.
where
SrcOctets: AsRef<[u8]>,
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: EmptyBuilder,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a blanket implementation from the previous PR. But it also converts ParsedDname -> Dname. I think it can either work like this, or or something like to_dname::<SrcOctets>()?.octets_into().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t just to_dname enough here? If this is about the error type – not sure why that is PushError in ToDname::to_dname. The type implementing ToDname should always have a valid domain name, so the only error is a ShortBuf. Unless I am missing something, I happily change the type in the trait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is so you can convert Record<ParsedDname<Src>, Src> to Record<Dname<Src>, Src> using octets conversion instead of rebuilding the record (which is useful when storing a record from message for example), but let me try to rebase without these changes and see if we need it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like there should be a better way to do this: if the name isn’t compressed, this can be done very cheaply for certain types. Maybe have a method flatten on ParseDname<_> and Record<ParseDname<_>, _>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that too! So how is flatten different from ToDname? It should be possible to optimize ToDname the same way (conversion is cheaper when as_flat_slice is some or octets can be shallow copied/referenced).

Should flatten be something like Dname::as_octets but return a an Option? (so for example ParsedDname<&Bytes> should return Option<&Bytes> which could be cheap to convert into Dname or Dname<&Bytes> as ParsedDname guarantees that the octets are a valid Dname). Is that what you mean? Or flatten would convert both type (ParsedDname -> Dname) and octets type (like this method)?

For me this is just a shortcut for building records from &[u8] and I rarely want to shallow copy Bytes backed records, so I'm also happy to drop this particular change, as it's just a convenience.

Copy link
Member

@partim partim May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea would be that if the parsed name is not compressed, flatten can simply return with self.parser.parse_octets(...) while otherwise it constructs the name via Dname::to_dname. I think this should be possible with slightly extravagant trait bounds – you’d need <Ref as OctetsRef>::Range to be FromBuilder plus some more things to tie it together.

Edit: I guess that wouldn’t actually work for your case, since you still need an octets conversion. Perhaps flatten_into instead with a type argument for a target octets type? I imagine it is cheaper to just copy the octets on an uncompressed name than constructing it label by label.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you want to proceed? I want to merge #130 for other work I am doing but there’s some overlap, so I would rather merge this PR first to avoid things getting too messy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been able to get back to this yet but let me decline it for now. I think I can make do without the nicer interface (or add extension traits to my library for record building with octets conversion) for now and will resubmit this if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Feel free to propose or request something at any time! I might implement the two proposed flatten methods anyway – that seems like a useful thing to have.

@partim
Copy link
Member

partim commented Apr 25, 2022

For my own understanding: Is this PR because Bytes doesn’t provide all necessary conversions via the From trait?

@vavrusa vavrusa closed this May 24, 2022
@xofyarg xofyarg mentioned this pull request Sep 30, 2022
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

Successfully merging this pull request may close these issues.

3 participants