Skip to content

Commit

Permalink
scaffoldig macro, sessiondeclaration wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Sep 8, 2024
1 parent 2620ff8 commit 7226b92
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 14 deletions.
83 changes: 82 additions & 1 deletion commons/zenoh-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
//! This crate is intended for Zenoh's internal use.
//!
//! [Click here for Zenoh's documentation](../zenoh/index.html)
use std::process::Output;

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, parse_quote, Attribute, Error, Item, LitStr, TraitItem};
use syn::{
parse_macro_input, parse_quote, Attribute, Error, Item, ItemImpl, Lifetime, LitStr, TraitItem,
};
use zenoh_keyexpr::{
format::{
macro_support::{self, SegmentBuilder},
Expand Down Expand Up @@ -522,3 +526,80 @@ pub fn register_param(input: proc_macro::TokenStream) -> proc_macro::TokenStream
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

///
/// Implements macro `#[scaffolding]` which precedes
/// `impl Trait for Struct { ... }``
///
/// This macro adds section `impl Struct` with the same funcations as in `impl Trait`
/// making methods with the same name as in trait be avaliable from struct directly,

Check warning on line 535 in commons/zenoh-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / Typos Check

"avaliable" should be "available".
/// without importing the trait.
///
#[proc_macro_attribute]
pub fn scaffolding(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemImpl);
let trait_path = &input.trait_.as_ref().unwrap().1;
let struct_path = &input.self_ty;
let generics = &input.generics;
// let struct_lifetime = get_type_path_lifetime(struct_path);

let mut struct_methods = quote! {};
for item_fn in input.items.iter() {
if let syn::ImplItem::Fn(method) = item_fn {
let method_name = &method.sig.ident;
let method_generic_params = &method.sig.generics.params;
let method_generic_params = if method_generic_params.is_empty() {
quote! {}
} else {
quote! {<#method_generic_params>}
};
// let method_name_str = method_name.to_string();
// let method_name_ident = syn::Ident::new(&method_name_str, method_name.span());
let method_args = &method.sig.inputs;
let method_output = &method.sig.output;
let where_clause = &method.sig.generics.where_clause;
let mut method_call_args = quote! { };
for arg in method_args.iter() {
match arg {
syn::FnArg::Receiver(_) => {
method_call_args.extend(quote! { self, });
}
syn::FnArg::Typed(pat_type) => {
let pat = &pat_type.pat;
method_call_args.extend(quote! { #pat, });
}
}
};
// call corresponding trait method from struct method
struct_methods.extend(quote! {
pub fn #method_name #method_generic_params (#method_args) #method_output #where_clause {
<#struct_path as #trait_path>::#method_name(#method_call_args)
}
});
}
}
let struct_methods_output = quote! {
impl #generics #struct_path {
#struct_methods
}
};

// let struct_methods_output = if let Some(struct_lifetime) = struct_lifetime {
// quote! {
// impl<#struct_lifetime> #struct_path {
// #struct_methods
// }
// }
// } else {
// quote! {
// impl #struct_path {
// #struct_methods
// }
// }
// };

(quote! {
#input
#struct_methods_output
}).into()
}
1 change: 0 additions & 1 deletion plugins/zenoh-plugin-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use zenoh::{
key_expr::{keyexpr, KeyExpr},
prelude::ZResult,
sample::Sample,
session::SessionDeclarations,
};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl};

Expand Down
1 change: 0 additions & 1 deletion plugins/zenoh-plugin-rest/examples/z_serve_sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use zenoh::{
config::Config,
key_expr::keyexpr,
qos::{CongestionControl, QoSBuilderTrait},
session::SessionDeclarations,
};

const HTML: &str = r#"
Expand Down
2 changes: 1 addition & 1 deletion plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use zenoh::{
prelude::*,
query::{Parameters, QueryConsolidation, Reply, Selector, ZenohParameters},
sample::{Sample, SampleKind},
session::{Session, SessionDeclarations},
session::{Session},
};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use zenoh::{
},
query::{ConsolidationMode, QueryTarget},
sample::{Sample, SampleBuilder, SampleKind, TimestampBuilderTrait},
session::{Session, SessionDeclarations},
session::{Session},
time::{Timestamp, NTP64},
};
use zenoh_backend_traits::{
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/api/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a> IntoFuture for PeersZenohIdBuilder<'a> {
}
}

/// Struct returned by [`Session::info()`](crate::session::SessionDeclarations::info) which allows
/// Struct returned by [`Session::info()`](crate::session::Session::info) which allows
/// to access information about the current zenoh [`Session`](crate::Session).
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/api/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ mod tests {
use zenoh_config::Config;
use zenoh_core::Wait;

use crate::api::{sample::SampleKind, session::SessionDeclarations};
use crate::api::{sample::SampleKind};

#[cfg(feature = "internal")]
#[test]
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/api/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ impl<'a, 'b, Handler> QueryableBuilder<'a, 'b, Handler> {
/// A queryable that provides data through a [`Handler`](crate::handlers::IntoHandler).
///
/// Queryables can be created from a zenoh [`Session`](crate::Session)
/// with the [`declare_queryable`](crate::session::SessionDeclarations::declare_queryable) function
/// with the [`declare_queryable`](crate::session::Session::declare_queryable) function
/// and the [`with`](QueryableBuilder::with) function
/// of the resulting builder.
///
Expand Down
5 changes: 4 additions & 1 deletion zenoh/src/api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use zenoh_buffers::ZBuf;
use zenoh_collections::SingleOrVec;
use zenoh_config::{unwrap_or_default, wrappers::ZenohId, Config, Notifier};
use zenoh_core::{zconfigurable, zread, Resolvable, Resolve, ResolveClosure, ResolveFuture, Wait};
use zenoh_macros::scaffolding;
#[cfg(feature = "unstable")]
use zenoh_protocol::network::{
declare::{DeclareToken, SubscriberId, TokenId, UndeclareToken},
Expand Down Expand Up @@ -365,6 +366,7 @@ pub enum SessionRef<'a> {
Shared(Arc<Session>),
}

#[scaffolding]
impl<'s, 'a> SessionDeclarations<'s, 'a> for SessionRef<'a> {
fn declare_subscriber<'b, TryIntoKeyExpr>(
&'s self,
Expand Down Expand Up @@ -706,8 +708,9 @@ impl Session {
}
}

#[scaffolding]
impl<'a> SessionDeclarations<'a, 'a> for Session {
fn info(&self) -> SessionInfo {
fn info(&'a self) -> SessionInfo<'a> {
SessionRef::Borrow(self).info()
}
fn declare_subscriber<'b, TryIntoKeyExpr>(
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/api/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl fmt::Debug for SubscriberState {
/// A subscriber that provides data through a callback.
///
/// CallbackSubscribers can be created from a zenoh [`Session`](crate::Session)
/// with the [`declare_subscriber`](crate::SessionDeclarations::declare_subscriber) function
/// with the [`declare_subscriber`](crate::session::Session::declare_subscriber) function
/// and the [`callback`](SubscriberBuilder::callback) function
/// of the resulting builder.
///
Expand Down Expand Up @@ -421,7 +421,7 @@ where
/// A subscriber that provides data through a [`Handler`](crate::handlers::IntoHandler).
///
/// Subscribers can be created from a zenoh [`Session`](crate::Session)
/// with the [`declare_subscriber`](crate::session::SessionDeclarations::declare_subscriber) function
/// with the [`declare_subscriber`](crate::session::Session::declare_subscriber) function
/// and the [`with`](SubscriberBuilder::with) function
/// of the resulting builder.
///
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub mod session {
builders::publisher::{SessionDeleteBuilder, SessionPutBuilder},
info::{PeersZenohIdBuilder, RoutersZenohIdBuilder, SessionInfo, ZenohIdBuilder},
query::SessionGetBuilder,
session::{open, OpenBuilder, Session, SessionDeclarations, SessionRef, Undeclarable},
session::{open, OpenBuilder, Session, SessionRef, Undeclarable},
};
}

Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod _prelude {
builders::sample::{
EncodingBuilderTrait, QoSBuilderTrait, SampleBuilderTrait, TimestampBuilderTrait,
},
session::{SessionDeclarations, Undeclarable},
session::{Undeclarable},
},
config::ValidatedMap,
Error as ZError, Resolvable, Resolve, Result as ZResult,
Expand Down
1 change: 0 additions & 1 deletion zenoh/tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async fn close_session(session: Session) {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn zenoh_events() {
use zenoh::prelude::SessionDeclarations;
let session = open_session(&["tcp/127.0.0.1:18447"], &[]).await;
let zid = session.zid();
let sub1 =
Expand Down

0 comments on commit 7226b92

Please sign in to comment.