Skip to content

Commit

Permalink
Add impls for Arc<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jan 29, 2025
1 parent f528afc commit 99dad52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions merde_core/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
hash::{BuildHasher, Hash},
marker::PhantomData,
pin::Pin,
sync::Arc,
};

use crate::{
Expand Down Expand Up @@ -486,6 +487,13 @@ impl<'s, T: Deserialize<'s>> Deserialize<'s> for Vec<T> {
}
}

impl<'s, T: Deserialize<'s>> Deserialize<'s> for Arc<T> {
async fn deserialize(de: &mut dyn DynDeserializer<'s>) -> Result<Self, MerdeError<'s>> {
let value: T = T::deserialize(de).await?;
Ok(Arc::new(value))
}
}

impl<'s, K, V, S> Deserialize<'s> for HashMap<K, V, S>
where
K: Deserialize<'s> + Eq + Hash,
Expand Down
10 changes: 10 additions & 0 deletions merde_core/src/into_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashSet;
use std::collections::VecDeque;
use std::hash::BuildHasher;
use std::hash::Hash;
use std::sync::Arc;

use crate::Event;

Expand Down Expand Up @@ -114,6 +115,15 @@ impl<T: IntoStatic> IntoStatic for Vec<T> {
}
}

impl<T: IntoStatic + Clone> IntoStatic for Arc<T> {
type Output = Arc<T::Output>;

fn into_static(self) -> Self::Output {
let t: T = (*self).clone();
Arc::new(t.into_static())
}
}

impl<K, V, S> IntoStatic for HashMap<K, V, S>
where
S: BuildHasher + Default + 'static,
Expand Down
13 changes: 12 additions & 1 deletion merde_core/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{borrow::Cow, collections::HashMap, future::Future, hash::BuildHasher, pin::Pin};
use std::{
borrow::Cow, collections::HashMap, future::Future, hash::BuildHasher, pin::Pin, sync::Arc,
};

use crate::{
metastack::MetastackExt, Array, ArrayStart, CowBytes, CowStr, Event, Map, MapStart, MerdeError,
Expand Down Expand Up @@ -211,6 +213,15 @@ impl<T: Serialize> Serialize for Vec<T> {
}
}

impl<T: Serialize> Serialize for Arc<T> {
async fn serialize<'se>(
&'se self,
serializer: &'se mut dyn DynSerializer,
) -> Result<(), MerdeError<'static>> {
(**self).serialize(serializer).await
}
}

impl<K: Serialize, V: Serialize, BH: BuildHasher> Serialize for HashMap<K, V, BH> {
async fn serialize<'fut>(
&'fut self,
Expand Down
8 changes: 8 additions & 0 deletions merde_core/src/with_lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
borrow::Cow,
collections::{HashMap, HashSet, VecDeque},
sync::Arc,
};

use crate::{CowStr, Value};
Expand Down Expand Up @@ -84,6 +85,13 @@ where
type Lifetimed = Vec<T::Lifetimed>;
}

impl<'s, T> WithLifetime<'s> for Arc<T>
where
T: WithLifetime<'s>,
{
type Lifetimed = Arc<T::Lifetimed>;
}

impl<'s, T> WithLifetime<'s> for VecDeque<T>
where
T: WithLifetime<'s>,
Expand Down

0 comments on commit 99dad52

Please sign in to comment.