Skip to content

Commit

Permalink
wip: service lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Nov 22, 2024
1 parent 361f05d commit a196c25
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
41 changes: 39 additions & 2 deletions rs/macros/core/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,51 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
});
}

let mut exposure_lifetimes: Punctuated<Lifetime, Comma> = Punctuated::new();
for lt in lifetimes.iter().map(|lt| {
let lt = format!("'{lt}");
Lifetime::new(&lt, Span::call_site())
}) {
exposure_lifetimes.push(lt);
}
let trait_lifetimes = if !exposure_lifetimes.is_empty() {
quote! { < #exposure_lifetimes> }
} else {
quote! {}
};

let mut base_types_funcs = Vec::with_capacity(service_args.base_types().len());
let mut base_types_impl = Vec::with_capacity(service_args.base_types().len());
let mut base_exposure_instantiation = Vec::with_capacity(service_args.base_types().len());
// let mut invocation_dispatches = Vec::with_capacity(service_handlers.len());
service_args.base_types().iter()
.enumerate()
.for_each(|(idx, base_type)| {
let as_base_ident = Ident::new(&format!("as_base_{}", idx), Span::call_site());

base_types_funcs.push(quote!{
fn #as_base_ident (&self) -> &< #base_type as #sails_path::gstd::services::Service>::Exposure;
});

base_types_impl.push(quote!{
fn #as_base_ident (&self) -> &< #base_type as #sails_path::gstd::services::Service>::Exposure {
&self.extend.#idx
}
});

base_exposure_instantiation.push(quote!(
< #base_type as Clone>::clone(AsRef::< #base_type >::as_ref( #inner_ident )).expose( #message_id_ident , #route_ident ),
));
});

quote!(
#service_impl

pub trait #trait_ident #exposure_lifetimes {
pub trait #trait_ident #trait_lifetimes {
#( #trait_funcs )*
}

impl #generics #trait_ident #exposure_lifetimes for #sails_path::gstd::services::ServiceExposure< #exposure_args, () > #service_type_constraints {
impl #generics #trait_ident #trait_lifetimes for #sails_path::gstd::services::ServiceExposure< #exposure_args, () > #service_type_constraints {
#( #trait_funcs_impl )*
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ impl<'a> ExtendedLifetime<'a> {
"extended".to_string()
}
}
trait __ExtendedLifetimeImplTrait {
pub trait ExtendedLifetimeImplTrait<'a> {
fn extended_name(&self) -> String;
fn name(&self) -> String;
}
impl<'a> __ExtendedLifetimeImplTrait
impl<'a> ExtendedLifetimeImplTrait<'a>
for sails_rs::gstd::services::ServiceExposure<'a, ExtendedLifetime<'a>, ()> {
fn extended_name(&self) -> String {
let exposure_scope = sails_rs::gstd::services::ExposureCallScope::new2(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ where
sails_rs::gstd::events::__notify_on(event)
}
}
pub trait MyGenericEventsServiceImplTrait {
pub trait MyGenericEventsServiceImplTrait<'a> {
fn do_this(&mut self) -> u32;
}
impl<'a, T> MyGenericEventsServiceImplTrait
impl<'a, T> MyGenericEventsServiceImplTrait<'a>
for sails_rs::gstd::services::ServiceExposure<MyGenericEventsService<'a, T>, ()>
where
T: Clone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ where
42
}
}
pub trait SomeServiceImplTrait {
pub trait SomeServiceImplTrait<'a, 'b> {
fn do_this(&mut self) -> u32;
}
impl<'a, 'b, T, U> SomeServiceImplTrait
impl<'a, 'b, T, U> SomeServiceImplTrait<'a, 'b>
for sails_rs::gstd::services::ServiceExposure<SomeService<'a, 'b, T, U>, ()>
where
T: Clone,
Expand Down
3 changes: 2 additions & 1 deletion rs/src/gstd/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct ServiceExposure<T, E> {
pub inner: Box<T>,
#[cfg(target_arch = "wasm32")]
pub inner: T,
extend: E,
pub extend: E,
}

impl<T, E> ServiceExposure<T, E> {
Expand Down Expand Up @@ -277,6 +277,7 @@ impl<T1: ServiceHandle, T2: ServiceHandle> ServiceHandle for (T1, T2) {
}
}

// todo: make macro_rules
impl<T1: ServiceHandle, T2: ServiceHandle, T3: ServiceHandle> ServiceHandle for (T1, T2, T3) {
async fn try_handle(&mut self, input: &[u8]) -> Option<(Vec<u8>, u128)> {
if let Some(result) = self.0.try_handle(input).await {
Expand Down

0 comments on commit a196c25

Please sign in to comment.