Skip to content

Commit

Permalink
Condition CGlueCtx to be both Send and Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Oct 7, 2021
1 parent e589a01 commit bf24eae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cglue-gen/src/func.rs
Expand Up @@ -743,11 +743,11 @@ impl ParsedFunc {
(
quote! {
let (this, cglue_ctx) = cont.cobj_base_owned();
let this = unsafe { crate::trait_group::IntoInner::into_inner(this) };
let this = unsafe { #trg_path::IntoInner::into_inner(this) };
#c_pre_call
},
Some(quote!(
CGlueC::InstType: crate::trait_group::IntoInner<InnerTarget = CGlueC::ObjType>,
CGlueC::InstType: #trg_path::IntoInner<InnerTarget = CGlueC::ObjType>,
)),
)
} else if self.receiver.mutability.is_some() {
Expand Down
4 changes: 2 additions & 2 deletions cglue-gen/src/traits.rs
Expand Up @@ -10,7 +10,7 @@ use syn::*;

// TODO: Add dynamic setting of Send / Sync
pub fn ctx_bound() -> TokenStream {
quote!('static + Clone) // + Send + Sync)
quote!('static + Clone + Send + Sync)
}

pub fn cglue_c_opaque_bound() -> TokenStream {
Expand Down Expand Up @@ -668,7 +668,7 @@ pub fn gen_trait(mut tr: ItemTrait, ext_name: Option<&Ident>) -> TokenStream {

let cglue_c_into_inner = if need_own {
Some(quote!(
CGlueC::InstType: crate::trait_group::IntoInner<InnerTarget = CGlueC::ObjType>,
CGlueC::InstType: #trg_path::IntoInner<InnerTarget = CGlueC::ObjType>,
))
} else {
None
Expand Down
37 changes: 24 additions & 13 deletions cglue/src/trait_group.rs
Expand Up @@ -123,8 +123,13 @@ pub type CGlueOpaqueTraitObj<'a, T, V> = CGlueTraitObj<
<V as CGlueBaseVtbl>::RetTmp,
>;

unsafe impl<'a, T: Opaquable, F: CGlueBaseVtbl<Context = C, RetTmp = R>, C: Clone, R: Default>
Opaquable for CGlueTraitObj<'a, T, F, C, R>
unsafe impl<
'a,
T: Opaquable,
F: CGlueBaseVtbl<Context = C, RetTmp = R>,
C: Clone + Send + Sync,
R: Default,
> Opaquable for CGlueTraitObj<'a, T, F, C, R>
{
type OpaqueTarget = CGlueTraitObj<'a, T::OpaqueTarget, F::OpaqueVtbl, C, R>;
}
Expand All @@ -142,7 +147,7 @@ impl<T, V, C, R> GetVtbl<V> for CGlueTraitObj<'_, T, V, C, R> {
// Conversions into container type itself.
// Needed when generated code returns Self

impl<'a, T: Deref<Target = F>, F, C: 'static + Clone, R: Default> From<(T, C)>
impl<'a, T: Deref<Target = F>, F, C: 'static + Clone + Send + Sync, R: Default> From<(T, C)>
for CGlueObjContainer<T, C, R>
{
fn from((instance, context): (T, C)) -> Self {
Expand All @@ -166,7 +171,9 @@ impl<'a, T, R: Default> From<T> for CGlueObjContainer<CBox<'a, T>, NoContext, R>
}
}

impl<'a, T, C: 'static + Clone, R: Default> From<(T, C)> for CGlueObjContainer<CBox<'a, T>, C, R> {
impl<'a, T, C: 'static + Clone + Send + Sync, R: Default> From<(T, C)>
for CGlueObjContainer<CBox<'a, T>, C, R>
{
fn from((this, context): (T, C)) -> Self {
Self::from((CBox::from(this), context))
}
Expand All @@ -177,7 +184,7 @@ impl<
T: Deref<Target = F>,
F,
V: CGlueVtbl<CGlueObjContainer<T, C, R>, Context = C, RetTmp = R>,
C: 'static + Clone,
C: 'static + Clone + Send + Sync,
R: Default,
> From<CGlueObjContainer<T, C, R>> for CGlueTraitObj<'a, T, V, V::Context, V::RetTmp>
where
Expand All @@ -196,7 +203,7 @@ impl<
T: Deref<Target = F>,
F,
V: CGlueVtbl<CGlueObjContainer<T, C, R>, Context = C, RetTmp = R>,
C: 'static + Clone,
C: 'static + Clone + Send + Sync,
R: Default,
> From<(T, V::Context)> for CGlueTraitObj<'a, T, V, V::Context, V::RetTmp>
where
Expand Down Expand Up @@ -240,7 +247,7 @@ impl<
'a,
T,
V: CGlueVtbl<CGlueObjContainer<CBox<'a, T>, C, R>, Context = C, RetTmp = R>,
C: 'static + Clone,
C: 'static + Clone + Send + Sync,
R: Default,
> From<(T, V::Context)> for CGlueTraitObj<'a, CBox<'a, T>, V, V::Context, V::RetTmp>
where
Expand All @@ -260,7 +267,7 @@ pub trait CGlueObjBase {
/// Type of the container housing the object.
type InstType: ::core::ops::Deref<Target = Self::ObjType>;
/// Type of the context associated with the container.
type Context: Clone + 'static;
type Context: 'static + Clone + Send + Sync;

fn cobj_base_ref(&self) -> (&Self::ObjType, &Self::Context);
fn cobj_base_owned(self) -> (Self::InstType, Self::Context);
Expand All @@ -270,7 +277,9 @@ pub trait CGlueObjRef<R>: CGlueObjBase {
fn cobj_ref(&self) -> (&Self::ObjType, &R, &Self::Context);
}

impl<T: Deref<Target = F>, F, C: Clone + 'static, R> CGlueObjBase for CGlueObjContainer<T, C, R> {
impl<T: Deref<Target = F>, F, C: 'static + Clone + Send + Sync, R> CGlueObjBase
for CGlueObjContainer<T, C, R>
{
type ObjType = F;
type InstType = T;
type Context = C;
Expand All @@ -284,7 +293,9 @@ impl<T: Deref<Target = F>, F, C: Clone + 'static, R> CGlueObjBase for CGlueObjCo
}
}

impl<T: Deref<Target = F>, F, C: Clone + 'static, R> CGlueObjRef<R> for CGlueObjContainer<T, C, R> {
impl<T: Deref<Target = F>, F, C: 'static + Clone + Send + Sync, R> CGlueObjRef<R>
for CGlueObjContainer<T, C, R>
{
fn cobj_ref(&self) -> (&F, &R, &Self::Context) {
(self.instance.deref(), &self.ret_tmp, &self.context)
}
Expand All @@ -297,7 +308,7 @@ pub trait CGlueObjMut<R>: CGlueObjRef<R> {
fn cobj_mut(&mut self) -> (&mut Self::ObjType, &mut R, &Self::Context);
}

impl<T: Deref<Target = F> + DerefMut, F, C: Clone + 'static, R> CGlueObjMut<R>
impl<T: Deref<Target = F> + DerefMut, F, C: 'static + Clone + Send + Sync, R> CGlueObjMut<R>
for CGlueObjContainer<T, C, R>
{
fn cobj_mut(&mut self) -> (&mut F, &mut R, &Self::Context) {
Expand All @@ -314,7 +325,7 @@ pub trait GetContainer {
fn build_with_ccont(&self, container: Self::ContType) -> Self;
}

impl<T: Deref<Target = F>, F, V, C: Clone + 'static, R> GetContainer
impl<T: Deref<Target = F>, F, V, C: 'static + Clone + Send + Sync, R> GetContainer
for CGlueTraitObj<'_, T, V, C, R>
{
type ContType = CGlueObjContainer<T, C, R>;
Expand Down Expand Up @@ -364,7 +375,7 @@ pub trait CGlueVtbl<T>: CGlueBaseVtbl {}
/// sure that the `OpaqueVtbl` is the exact same type, with the only difference being `this` types.
pub unsafe trait CGlueBaseVtbl: Sized {
type OpaqueVtbl: Sized;
type Context: Sized + Clone;
type Context: Sized + Clone + Send + Sync;
type RetTmp: Sized + Default;

/// Get the opaque vtable for the type.
Expand Down

0 comments on commit bf24eae

Please sign in to comment.