mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-02-15 22:18:20 +01:00
Mangle a lot of names in macro expansion. (#53)
* Mangle a lot of names in macro expansion.
To lower the chance of any issues, prefix idents in service expansion with __tarpc_service.
In future_enum, prefix with __future_enum. The pattern is basically __macro_name_ident.
Any imported enum variant will conflict with a let binding or a function arg, so we basically
can't use any generic idents at all. Example:
enum Req { request(..) }
use self::Req::request;
fn make_request(request: Request) { ... }
^^^^^^^ conflict here
Additionally, suffix generated associated types with Fut to avoid conflicts with camelcased rpcs.
Why someone would do that, I don't know, but we shouldn't allow that wart.
* Trim long macro lines
This commit is contained in:
152
src/macros.rs
152
src/macros.rs
@@ -82,7 +82,8 @@ macro_rules! impl_serialize {
|
|||||||
as_item! {
|
as_item! {
|
||||||
impl$($lifetime)* $crate::serde::Serialize for $impler$($lifetime)* {
|
impl$($lifetime)* $crate::serde::Serialize for $impler$($lifetime)* {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, __impl_serialize_serializer: &mut S) -> ::std::result::Result<(), S::Error>
|
fn serialize<S>(&self, __impl_serialize_serializer: &mut S)
|
||||||
|
-> ::std::result::Result<(), S::Error>
|
||||||
where S: $crate::serde::Serializer
|
where S: $crate::serde::Serializer
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
@@ -124,7 +125,8 @@ macro_rules! impl_deserialize {
|
|||||||
($impler:ident, $(@($name:ident $n:expr))* -- #($_n:expr) ) => (
|
($impler:ident, $(@($name:ident $n:expr))* -- #($_n:expr) ) => (
|
||||||
impl $crate::serde::Deserialize for $impler {
|
impl $crate::serde::Deserialize for $impler {
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
fn deserialize<__impl_deserialize_D>(__impl_deserialize_deserializer: &mut __impl_deserialize_D)
|
fn deserialize<__impl_deserialize_D>(
|
||||||
|
__impl_deserialize_deserializer: &mut __impl_deserialize_D)
|
||||||
-> ::std::result::Result<$impler, __impl_deserialize_D::Error>
|
-> ::std::result::Result<$impler, __impl_deserialize_D::Error>
|
||||||
where __impl_deserialize_D: $crate::serde::Deserializer
|
where __impl_deserialize_D: $crate::serde::Deserializer
|
||||||
{
|
{
|
||||||
@@ -150,16 +152,19 @@ macro_rules! impl_deserialize {
|
|||||||
{
|
{
|
||||||
$(
|
$(
|
||||||
if __impl_deserialize_value == $n {
|
if __impl_deserialize_value == $n {
|
||||||
return ::std::result::Result::Ok(__impl_deserialize_Field::$name);
|
return ::std::result::Result::Ok(
|
||||||
|
__impl_deserialize_Field::$name);
|
||||||
}
|
}
|
||||||
)*
|
)*
|
||||||
::std::result::Result::Err(
|
::std::result::Result::Err(
|
||||||
$crate::serde::de::Error::custom(
|
$crate::serde::de::Error::custom(
|
||||||
format!("No variants have a value of {}!", __impl_deserialize_value))
|
format!("No variants have a value of {}!",
|
||||||
|
__impl_deserialize_value))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
__impl_deserialize_deserializer.deserialize_struct_field(__impl_deserialize_FieldVisitor)
|
__impl_deserialize_deserializer.deserialize_struct_field(
|
||||||
|
__impl_deserialize_FieldVisitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +192,8 @@ macro_rules! impl_deserialize {
|
|||||||
stringify!($name)
|
stringify!($name)
|
||||||
),*
|
),*
|
||||||
];
|
];
|
||||||
__impl_deserialize_deserializer.deserialize_enum(stringify!($impler), VARIANTS, Visitor)
|
__impl_deserialize_deserializer.deserialize_enum(
|
||||||
|
stringify!($impler), VARIANTS, Visitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -408,11 +414,13 @@ macro_rules! service {
|
|||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
enum __tarpc_service_Reply<__tarpc_service_S: FutureService> {
|
enum __tarpc_service_Reply<__tarpc_service_S: FutureService> {
|
||||||
DeserializeError($crate::SerializeFuture),
|
DeserializeError($crate::SerializeFuture),
|
||||||
$($fn_name($crate::futures::Then<$crate::futures::MapErr<ty_snake_to_camel!(__tarpc_service_S::$fn_name),
|
$($fn_name($crate::futures::Then<
|
||||||
fn($error) -> $crate::WireError<$error>>,
|
$crate::futures::MapErr<
|
||||||
$crate::SerializeFuture,
|
ty_snake_to_camel!(__tarpc_service_S::$fn_name),
|
||||||
fn(::std::result::Result<$out, $crate::WireError<$error>>)
|
fn($error) -> $crate::WireError<$error>>,
|
||||||
-> $crate::SerializeFuture>)),*
|
$crate::SerializeFuture,
|
||||||
|
fn(::std::result::Result<$out, $crate::WireError<$error>>)
|
||||||
|
-> $crate::SerializeFuture>)),*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: FutureService> $crate::futures::Future for __tarpc_service_Reply<S> {
|
impl<S: FutureService> $crate::futures::Future for __tarpc_service_Reply<S> {
|
||||||
@@ -421,15 +429,22 @@ macro_rules! service {
|
|||||||
|
|
||||||
fn poll(&mut self) -> $crate::futures::Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> $crate::futures::Poll<Self::Item, Self::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
__tarpc_service_Reply::DeserializeError(ref mut f) => $crate::futures::Future::poll(f),
|
__tarpc_service_Reply::DeserializeError(ref mut f) => {
|
||||||
$(__tarpc_service_Reply::$fn_name(ref mut f) => $crate::futures::Future::poll(f)),*
|
$crate::futures::Future::poll(f)
|
||||||
|
}
|
||||||
|
$(
|
||||||
|
__tarpc_service_Reply::$fn_name(ref mut f) => {
|
||||||
|
$crate::futures::Future::poll(f)
|
||||||
|
}
|
||||||
|
),*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
impl<__tarpc_service_S> $crate::tokio_service::Service for __tarpc_service_AsyncServer<__tarpc_service_S>
|
impl<__tarpc_service_S> $crate::tokio_service::Service
|
||||||
|
for __tarpc_service_AsyncServer<__tarpc_service_S>
|
||||||
where __tarpc_service_S: FutureService
|
where __tarpc_service_S: FutureService
|
||||||
{
|
{
|
||||||
type Request = ::std::vec::Vec<u8>;
|
type Request = ::std::vec::Vec<u8>;
|
||||||
@@ -441,7 +456,7 @@ macro_rules! service {
|
|||||||
$crate::futures::Async::Ready(())
|
$crate::futures::Async::Ready(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&self, req: Self::Request) -> Self::Future {
|
fn call(&self, __tarpc_service_req: Self::Request) -> Self::Future {
|
||||||
#[allow(non_camel_case_types, unused)]
|
#[allow(non_camel_case_types, unused)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum __tarpc_service_ServerSideRequest {
|
enum __tarpc_service_ServerSideRequest {
|
||||||
@@ -450,32 +465,48 @@ macro_rules! service {
|
|||||||
),*
|
),*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_deserialize!(__tarpc_service_ServerSideRequest, $($fn_name(($($in_),*)))*);
|
impl_deserialize!(__tarpc_service_ServerSideRequest,
|
||||||
|
$($fn_name(($($in_),*)))*);
|
||||||
|
|
||||||
let __tarpc_service_request = $crate::deserialize(&req);
|
let __tarpc_service_request = $crate::deserialize(&__tarpc_service_req);
|
||||||
let __tarpc_service_request: __tarpc_service_ServerSideRequest = match __tarpc_service_request {
|
let __tarpc_service_request: __tarpc_service_ServerSideRequest =
|
||||||
::std::result::Result::Ok(__tarpc_service_request) => __tarpc_service_request,
|
match __tarpc_service_request {
|
||||||
::std::result::Result::Err(__tarpc_service_e) => {
|
::std::result::Result::Ok(__tarpc_service_request) => {
|
||||||
return __tarpc_service_Reply::DeserializeError(deserialize_error(__tarpc_service_e));
|
__tarpc_service_request
|
||||||
}
|
}
|
||||||
};
|
::std::result::Result::Err(__tarpc_service_e) => {
|
||||||
|
return __tarpc_service_Reply::DeserializeError(
|
||||||
|
deserialize_error(__tarpc_service_e));
|
||||||
|
}
|
||||||
|
};
|
||||||
match __tarpc_service_request {$(
|
match __tarpc_service_request {$(
|
||||||
__tarpc_service_ServerSideRequest::$fn_name(( $($arg,)* )) => {
|
__tarpc_service_ServerSideRequest::$fn_name(( $($arg,)* )) => {
|
||||||
const SERIALIZE: fn(::std::result::Result<$out, $crate::WireError<$error>>)
|
const SERIALIZE:
|
||||||
-> $crate::SerializeFuture = $crate::serialize_reply;
|
fn(::std::result::Result<$out, $crate::WireError<$error>>)
|
||||||
const TO_APP: fn($error) -> $crate::WireError<$error> = $crate::WireError::App;
|
-> $crate::SerializeFuture
|
||||||
|
= $crate::serialize_reply;
|
||||||
|
const TO_APP: fn($error) -> $crate::WireError<$error> =
|
||||||
|
$crate::WireError::App;
|
||||||
|
|
||||||
let reply = FutureService::$fn_name(&self.0, $($arg),*);
|
return __tarpc_service_Reply::$fn_name(
|
||||||
let reply = $crate::futures::Future::map_err(reply, TO_APP);
|
$crate::futures::Future::then(
|
||||||
let reply = $crate::futures::Future::then(reply, SERIALIZE);
|
$crate::futures::Future::map_err(
|
||||||
return __tarpc_service_Reply::$fn_name(reply);
|
FutureService::$fn_name(&self.0, $($arg),*),
|
||||||
|
TO_APP),
|
||||||
|
SERIALIZE));
|
||||||
}
|
}
|
||||||
)*}
|
)*}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize_error<E: ::std::error::Error>(__tarpc_service_e: E) -> $crate::SerializeFuture {
|
fn deserialize_error<E: ::std::error::Error>(__tarpc_service_e: E)
|
||||||
let __tarpc_service_err = $crate::WireError::ServerDeserialize::<$crate::util::Never>(__tarpc_service_e.to_string());
|
-> $crate::SerializeFuture
|
||||||
$crate::serialize_reply(::std::result::Result::Err::<(), _>(__tarpc_service_err))
|
{
|
||||||
|
$crate::serialize_reply(
|
||||||
|
// The type param is only used in the Error::App variant, so it
|
||||||
|
// doesn't matter what we specify it as here.
|
||||||
|
::std::result::Result::Err::<(), _>(
|
||||||
|
$crate::WireError::ServerDeserialize::<$crate::util::Never>(
|
||||||
|
__tarpc_service_e.to_string())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -506,10 +537,10 @@ macro_rules! service {
|
|||||||
where L: ::std::net::ToSocketAddrs
|
where L: ::std::net::ToSocketAddrs
|
||||||
{
|
{
|
||||||
|
|
||||||
let service = __SyncServer {
|
let __tarpc_service_service = __SyncServer {
|
||||||
service: self,
|
service: self,
|
||||||
};
|
};
|
||||||
return ::std::result::Result::unwrap($crate::futures::Future::wait(FutureServiceExt::listen(service, addr)));
|
return ::std::result::Result::unwrap($crate::futures::Future::wait(FutureServiceExt::listen(__tarpc_service_service, addr)));
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct __SyncServer<S> {
|
struct __SyncServer<S> {
|
||||||
@@ -534,17 +565,22 @@ macro_rules! service {
|
|||||||
// TODO(tikue): what do do if SyncService panics?
|
// TODO(tikue): what do do if SyncService panics?
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
let (__tarpc_service_complete, __tarpc_service_promise) =
|
||||||
|
$crate::futures::oneshot();
|
||||||
|
let __tarpc_service_service = self.clone();
|
||||||
const UNIMPLEMENTED: fn($crate::futures::Canceled) -> $error =
|
const UNIMPLEMENTED: fn($crate::futures::Canceled) -> $error =
|
||||||
unimplemented;
|
unimplemented;
|
||||||
|
|
||||||
let (c, p) = $crate::futures::oneshot();
|
|
||||||
let service = self.clone();
|
|
||||||
::std::thread::spawn(move || {
|
::std::thread::spawn(move || {
|
||||||
let reply = SyncService::$fn_name(&service.service, $($arg),*);
|
let __tarpc_service_reply = SyncService::$fn_name(
|
||||||
c.complete($crate::futures::IntoFuture::into_future(reply));
|
&__tarpc_service_service.service, $($arg),*);
|
||||||
|
__tarpc_service_complete.complete(
|
||||||
|
$crate::futures::IntoFuture::into_future(
|
||||||
|
__tarpc_service_reply));
|
||||||
});
|
});
|
||||||
let p = $crate::futures::Future::map_err(p, UNIMPLEMENTED);
|
let __tarpc_service_promise =
|
||||||
$crate::futures::Future::flatten(p)
|
$crate::futures::Future::map_err(
|
||||||
|
__tarpc_service_promise, UNIMPLEMENTED);
|
||||||
|
$crate::futures::Future::flatten(__tarpc_service_promise)
|
||||||
}
|
}
|
||||||
)*
|
)*
|
||||||
}
|
}
|
||||||
@@ -615,7 +651,8 @@ macro_rules! service {
|
|||||||
$(#[$attr])*
|
$(#[$attr])*
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn $fn_name(&self, $($arg: &$in_),*)
|
pub fn $fn_name(&self, $($arg: &$in_),*)
|
||||||
-> impl $crate::futures::Future<Item=$out, Error=$crate::Error<$error>> + 'static
|
-> impl $crate::futures::Future<Item=$out, Error=$crate::Error<$error>>
|
||||||
|
+ 'static
|
||||||
{
|
{
|
||||||
$client_req
|
$client_req
|
||||||
$client_serialize_impl
|
$client_serialize_impl
|
||||||
@@ -629,18 +666,33 @@ macro_rules! service {
|
|||||||
|
|
||||||
let __tarpc_service_args = ($($arg,)*);
|
let __tarpc_service_args = ($($arg,)*);
|
||||||
let __tarpc_service_req = &__ClientSideRequest::$fn_name(&__tarpc_service_args);
|
let __tarpc_service_req = &__ClientSideRequest::$fn_name(&__tarpc_service_args);
|
||||||
let __tarpc_service_req = match $crate::Packet::serialize(&__tarpc_service_req) {
|
let __tarpc_service_req =
|
||||||
::std::result::Result::Err(__tarpc_service_e) => return Fut::Failed($crate::futures::failed($crate::Error::ClientSerialize(__tarpc_service_e))),
|
match $crate::Packet::serialize(&__tarpc_service_req)
|
||||||
|
{
|
||||||
|
::std::result::Result::Err(__tarpc_service_e) => {
|
||||||
|
return Fut::Failed(
|
||||||
|
$crate::futures::failed(
|
||||||
|
$crate::Error::ClientSerialize(__tarpc_service_e)))
|
||||||
|
}
|
||||||
::std::result::Result::Ok(__tarpc_service_req) => __tarpc_service_req,
|
::std::result::Result::Ok(__tarpc_service_req) => __tarpc_service_req,
|
||||||
};
|
};
|
||||||
let __tarpc_service_fut = $crate::tokio_service::Service::call(&self.0, __tarpc_service_req);
|
let __tarpc_service_fut =
|
||||||
Fut::Called($crate::futures::Future::then(__tarpc_service_fut, move |__tarpc_service_msg| {
|
$crate::tokio_service::Service::call(&self.0, __tarpc_service_req);
|
||||||
|
Fut::Called($crate::futures::Future::then(__tarpc_service_fut,
|
||||||
|
move |__tarpc_service_msg| {
|
||||||
let __tarpc_service_msg: Vec<u8> = try!(__tarpc_service_msg);
|
let __tarpc_service_msg: Vec<u8> = try!(__tarpc_service_msg);
|
||||||
let __tarpc_service_msg: ::std::result::Result<::std::result::Result<$out, $crate::WireError<$error>>, _>
|
let __tarpc_service_msg:
|
||||||
|
::std::result::Result<
|
||||||
|
::std::result::Result<$out, $crate::WireError<$error>>, _>
|
||||||
= $crate::deserialize(&__tarpc_service_msg);
|
= $crate::deserialize(&__tarpc_service_msg);
|
||||||
match __tarpc_service_msg {
|
match __tarpc_service_msg {
|
||||||
::std::result::Result::Ok(__tarpc_service_msg) => ::std::result::Result::Ok(try!(__tarpc_service_msg)),
|
::std::result::Result::Ok(__tarpc_service_msg) => {
|
||||||
::std::result::Result::Err(__tarpc_service_e) => ::std::result::Result::Err($crate::Error::ClientDeserialize(__tarpc_service_e)),
|
::std::result::Result::Ok(try!(__tarpc_service_msg))
|
||||||
|
}
|
||||||
|
::std::result::Result::Err(__tarpc_service_e) => {
|
||||||
|
::std::result::Result::Err(
|
||||||
|
$crate::Error::ClientDeserialize(__tarpc_service_e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ use syntax::ast::LitKind::Str;
|
|||||||
use syntax::ast::MetaItemKind::NameValue;
|
use syntax::ast::MetaItemKind::NameValue;
|
||||||
use syntax::codemap::Spanned;
|
use syntax::codemap::Spanned;
|
||||||
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
|
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
|
||||||
|
use syntax::ext::quote::rt::Span;
|
||||||
use syntax::parse::{self, token, PResult};
|
use syntax::parse::{self, token, PResult};
|
||||||
use syntax::ptr::P;
|
|
||||||
use syntax::parse::parser::{Parser, PathStyle};
|
use syntax::parse::parser::{Parser, PathStyle};
|
||||||
use syntax::parse::token::intern_and_get_ident;
|
use syntax::parse::token::intern_and_get_ident;
|
||||||
|
use syntax::ptr::P;
|
||||||
use syntax::tokenstream::TokenTree;
|
use syntax::tokenstream::TokenTree;
|
||||||
use syntax::ext::quote::rt::Span;
|
|
||||||
use syntax::util::small_vector::SmallVector;
|
use syntax::util::small_vector::SmallVector;
|
||||||
|
|
||||||
fn snake_to_camel(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'static> {
|
fn snake_to_camel(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'static> {
|
||||||
|
|||||||
Reference in New Issue
Block a user