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 trait FlattenInto #151

Merged
merged 1 commit into from
Oct 6, 2022
Merged

add trait FlattenInto #151

merged 1 commit into from
Oct 6, 2022

Conversation

xofyarg
Copy link
Contributor

@xofyarg xofyarg commented Sep 30, 2022

Hi,

This is a followup for #126, could you please let me know what do you think when you got a moment? Thanks.

The FlattenInto trait can help in case of converting ParsedDname into Dname in nested structure like Record<N1, ZoneRecordData<O, N2>>, so the user does not need to enumerate all the rdata types.

@partim
Copy link
Member

partim commented Oct 3, 2022

Thank you for the PR!

I wonder if we can get away without having a dedicated trait for this in rather offer a flatten_into method on the types that use a ParsedDname? Looking briefly, the only tricky bit seems to be record data. Maybe we can add flatten_into to the ParseRecordData trait?

@xofyarg
Copy link
Contributor Author

xofyarg commented Oct 4, 2022

I think the trait is not really necessary, but easy to enforce that the method is implemented correctly everywhere. I updated the PR, and removed the trait. Although I didn't find a proper way to add a flatten_into method to Record, but I'm Ok without it. Please let me know what do you think.

@partim
Copy link
Member

partim commented Oct 4, 2022

Given that flatten_into only makes sense for ParsedDname, how about only providing it for record data types that are generic over a name when the name is ParsedDname? E.g.:

        impl<SrcOctets, Ref> ZoneRecordData<SrcOctets, ParseDname<Ref>>
        where
            SrcOctets: AsRef<[u8]>,
            Ref: OctetsRef,
        {
            pub fn flatten_into<Octets>(
                self,
            ) -> Result<ZoneRecordData<Octets, crate::base::Dname<Octets>>, PushError>
            where
                Octets: OctetsFrom<SrcOctets> + FromBuilder,
                <Octets as FromBuilder>::Builder: EmptyBuilder,
            {
                // ...
            }
        }

You can then also provide flatten_into on ParsedDname, which will probably make the impls more straightforward.

What would then also be good if the result uses Dname<Ref::Range> in the above example. Then you can simply copy out the range in case the name isn’t compressed rather than constructing a new name like ToDname does.

@xofyarg
Copy link
Contributor Author

xofyarg commented Oct 4, 2022

I can drop the ToDname, and use ParsedDname instead.

What would then also be good if the result uses DnameRef::Range in the above example. Then you can simply copy out the range in case the name isn’t compressed rather than constructing a new name like ToDname does.

Not quit sure about this though, if we'd like to optimize flatten_into for case that the name is not compressed, then the result would be different when compression is used. I think it's situational only in case of converting ParsedDname<&Bytes> to Dname<Bytes> so that the ToDname could be saved in uncompressed case. Or if I missed anything?

@partim
Copy link
Member

partim commented Oct 5, 2022

A Cow<[u8]> would be another option to keep uncompressed names cheap.

Can we use OctetsFrom for this purpose? The method’s name of flatten_into suggests that that might be the case. Require the target’s octets type to be OctetsFrom<Ref::Range> + OctetsBuilder?

The function converts ParsedDname inside rdata into Dname, with proper
octets change. It helps the user from making the boilerplate again on
each of the individual types.
@xofyarg
Copy link
Contributor Author

xofyarg commented Oct 6, 2022

I think I get your idea now. PR updated.

@partim
Copy link
Member

partim commented Oct 6, 2022

Looking great now! Again, thank you for the PR!

@partim partim merged commit a148380 into NLnetLabs:main Oct 6, 2022
@xofyarg xofyarg deleted the flatten branch October 6, 2022 15:27
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.

2 participants