Skip to content

Commit

Permalink
refactor UniqueVecAdapter to use associated types
Browse files Browse the repository at this point in the history
  • Loading branch information
nklhtv committed Jun 16, 2022
1 parent 99ba5db commit c9a722b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
16 changes: 7 additions & 9 deletions src/types/addon/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ pub struct Manifest {
pub resources: Vec<ManifestResource>,
pub id_prefixes: Option<Vec<String>>,
#[serde(default)]
#[serde_as(
as = "UniqueVec<ManifestCatalog, (String, String), ManifestCatalogUniqueVecAdapter>"
)]
#[serde_as(as = "UniqueVec<ManifestCatalogUniqueVecAdapter>")]
pub catalogs: Vec<ManifestCatalog>,
#[serde(default)]
#[serde_as(
as = "UniqueVec<ManifestCatalog, (String, String), ManifestCatalogUniqueVecAdapter>"
)]
#[serde_as(as = "UniqueVec<ManifestCatalogUniqueVecAdapter>")]
pub addon_catalogs: Vec<ManifestCatalog>,
#[serde(default)]
pub behavior_hints: ManifestBehaviorHints,
Expand Down Expand Up @@ -168,9 +164,11 @@ impl ManifestCatalog {

struct ManifestCatalogUniqueVecAdapter;

impl UniqueVecAdapter<ManifestCatalog, (String, String)> for ManifestCatalogUniqueVecAdapter {
fn hash(value: &ManifestCatalog) -> (String, String) {
(value.id.to_owned(), value.r#type.to_owned())
impl UniqueVecAdapter for ManifestCatalogUniqueVecAdapter {
type Input = ManifestCatalog;
type Output = (String, String);
fn hash(catalog: &ManifestCatalog) -> (String, String) {
(catalog.id.to_owned(), catalog.r#type.to_owned())
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/types/profile/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub type UID = Option<String>;
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct Profile {
pub auth: Option<Auth>,
#[serde_as(as = "UniqueVec<Descriptor, Url, DescriptorUniqueVecAdapter>")]
#[serde_as(as = "UniqueVec<DescriptorUniqueVecAdapter>")]
pub addons: Vec<Descriptor>,
pub settings: Settings,
}
Expand All @@ -39,8 +39,10 @@ impl Profile {

struct DescriptorUniqueVecAdapter;

impl UniqueVecAdapter<Descriptor, Url> for DescriptorUniqueVecAdapter {
fn hash(value: &Descriptor) -> Url {
value.transport_url.to_owned()
impl UniqueVecAdapter for DescriptorUniqueVecAdapter {
type Input = Descriptor;
type Output = Url;
fn hash(descriptor: &Descriptor) -> Url {
descriptor.transport_url.to_owned()
}
}
17 changes: 10 additions & 7 deletions src/types/serde_as_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::hash::Hash;

pub trait UniqueVecAdapter<T, U: Eq + Hash> {
fn hash(value: &T) -> U;
pub trait UniqueVecAdapter {
type Input;
type Output: Eq + Hash;

fn hash(value: &Self::Input) -> Self::Output;
}

#[derive(Copy, Clone, Debug, Default)]
pub struct UniqueVec<T, U, A>(PhantomData<(T, U, A)>);
pub struct UniqueVec<A>(PhantomData<A>);

impl<'de, T, U, A> DeserializeAs<'de, Vec<T>> for UniqueVec<T, U, A>
impl<'de, T, U, A> DeserializeAs<'de, Vec<T>> for UniqueVec<A>
where
T: Deserialize<'de>,
U: Eq + Hash,
A: UniqueVecAdapter<T, U>,
A: UniqueVecAdapter<Input = T, Output = U>,
{
fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
Expand All @@ -26,11 +29,11 @@ where
}
}

impl<T, U, A> SerializeAs<Vec<T>> for UniqueVec<T, U, A>
impl<T, U, A> SerializeAs<Vec<T>> for UniqueVec<A>
where
T: Serialize,
U: Eq + Hash,
A: UniqueVecAdapter<T, U>,
A: UniqueVecAdapter<Input = T, Output = U>,
{
fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit c9a722b

Please sign in to comment.