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

spki: add AlgorithmIdentifierRef::to_owned method #796

Closed

Conversation

baloo
Copy link
Member

@baloo baloo commented Dec 13, 2022

No description provided.

@tarcieri
Copy link
Member

Perhaps we can define some traits for this, since we can't use std::borrow::ToOwned (since this isn't a reference conversion).

It would be nice to ensure there's always a reciprocal operation ala std::borrow::Borrow.

@baloo
Copy link
Member Author

baloo commented Dec 14, 2022

I'm pretty bad at naming things, but any opinion?

pub trait DerBorrow {
    type Borrowed<'b>
    where
        Self: 'b;

    fn der_borrow<'a>(&'a self) -> Self::Borrowed<'a>;
}

pub trait DerToOwned<'a> {
    type DerOwned: DerBorrow<Borrowed<'a> = Self>
    where
        Self: 'a;

    fn to_der_owned(&self) -> Self::DerOwned;
}

impl<'a> DerToOwned<'a> for AlgorithmIdentifierRef<'a> {
    type DerOwned = AlgorithmIdentifierOwned;

    fn to_der_owned(&self) -> Self::DerOwned {
        AlgorithmIdentifier {
            oid: self.oid,
            parameters: self.parameters.map(|p| p.into()),
        }
    }
}

impl DerBorrow for AlgorithmIdentifierOwned {
    type Borrowed<'a> = AlgorithmIdentifierRef<'a>;

    fn der_borrow<'a>(&'a self) -> AlgorithmIdentifierRef<'a> {
        AlgorithmIdentifier {
            oid: self.oid,
            parameters: self.parameters.as_ref().map(AnyRef::from),
        }
    }
}

Compiler seems happy.

@tarcieri
Copy link
Member

That looks okay naming aside. Let me think about that for a bit.

@tarcieri
Copy link
Member

Naming wise, for starters, the "borrowed" types are owned *Ref types, so perhaps ToRef instead of ToBorrowed?

@baloo
Copy link
Member Author

baloo commented Dec 14, 2022

pub trait OwnedToRef {
    type Borrowed<'b>
    where
        Self: 'b;

    fn to_ref<'a>(&'a self) -> Self::Borrowed<'a>;
}

pub trait RefToOwned<'a> {
    type Owned: OwnedToRef<Borrowed<'a> = Self>
    where
        Self: 'a;

    fn to_owned(&self) -> Self::Owned;
}

@tarcieri
Copy link
Member

Looking better… maybe RefToOwned and OwnedToRef?

tarcieri pushed a commit that referenced this pull request Dec 17, 2022
This introduces the two new traits to go from a ref object to an owned object as discussed in #796
@baloo baloo deleted the baloo/spki/algorithm-identifer.owned branch December 18, 2022 03:18
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