Skip to content

Commit

Permalink
Macro hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorstmann committed Dec 31, 2024
1 parent c013eaa commit 1807f8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions parquet/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use compact_thrift_rs::*;
use compact_thrift_rs::macros::*;
#![allow(non_snake_case)]
use compact_thrift_rs::thrift;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down
61 changes: 29 additions & 32 deletions runtime/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use crate::{ThriftError};
use crate::protocol::{ CompactThriftInput, CompactThriftOutput, CompactThriftProtocol};

#[macro_export]
macro_rules! thrift {
($(#[$($def_attrs:tt)*])* struct $identifier:ident { $($definitions:tt)* } $($remainder:tt)*) => {
thrift_struct!($(#[$($def_attrs)*])* struct $identifier { $($definitions)* });
thrift!($($remainder)*);
$crate::thrift_struct!($(#[$($def_attrs)*])* struct $identifier { $($definitions)* });
$crate::thrift!($($remainder)*);
};
($(#[$($def_attrs:tt)*])* union $identifier:ident { $($definitions:tt)* } $($remainder:tt)*) => {
thrift_union!($(#[$($def_attrs)*])* union $identifier { $($definitions)* });
thrift!($($remainder)*);
$crate::thrift_union!($(#[$($def_attrs)*])* union $identifier { $($definitions)* });
$crate::thrift!($($remainder)*);
};
($(#[$($def_attrs:tt)*])* enum $identifier:ident { $($definitions:tt)* } $($remainder:tt)*) => {
thrift_enum!($(#[$($def_attrs)*])* enum $identifier { $($definitions)* });
thrift!($($remainder)*);
$crate::thrift_enum!($(#[$($def_attrs)*])* enum $identifier { $($definitions)* });
$crate::thrift!($($remainder)*);
};
($(#[$($def_attrs:tt)*])* namespace $identifier:ident $namespace:ident $($remainder:tt)*) => {
thrift!($($remainder)*);
$crate::thrift!($($remainder)*);
};

() => {};
Expand All @@ -30,25 +27,25 @@ macro_rules! thrift_struct {
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct $identifier {
$($(#[cfg_attr(not(doctest), $($field_attrs)*)])* pub $field_name: required_or_optional!($required_or_optional field_type!($field_type $($element_type)?))),*
$($(#[cfg_attr(not(doctest), $($field_attrs)*)])* pub $field_name: $crate::required_or_optional!($required_or_optional $crate::field_type!($field_type $($element_type)?))),*
}

impl $identifier {
pub fn new($($field_name: impl Into<required_or_optional!($required_or_optional field_type!($field_type $($element_type)?))>),*) -> Self {
pub fn new($($field_name: impl Into<$crate::required_or_optional!($required_or_optional $crate::field_type!($field_type $($element_type)?))>),*) -> Self {
Self {
$($field_name: $field_name.into(),)*
}
}
}

#[allow(non_snake_case)]
impl <'i> CompactThriftProtocol<'i> for $identifier {
impl <'i> $crate::CompactThriftProtocol<'i> for $identifier {
const FIELD_TYPE: u8 = 12;

#[inline(never)]
fn fill<T: CompactThriftInput<'i>>(&mut self, input: &mut T) -> Result<(), ThriftError> {
fn fill<T: $crate::CompactThriftInput<'i>>(&mut self, input: &mut T) -> std::result::Result<(), $crate::ThriftError> {
let mut last_field_id = 0_i16;
$(required_flag!($required_or_optional $field_name);)*
$($crate::required_flag!($required_or_optional $field_name);)*
loop {
let field_type = input.read_field_header(&mut last_field_id)?;
if field_type == 0 {
Expand All @@ -57,7 +54,7 @@ macro_rules! thrift_struct {

match last_field_id {
$($field_id => {
required_set!($required_or_optional $field_name);
$crate::required_set!($required_or_optional $field_name);
self.$field_name.fill_field(input, field_type)?;
}),*
_ => {
Expand All @@ -66,12 +63,12 @@ macro_rules! thrift_struct {
}
}

$(required_check!($required_or_optional $identifier $field_name);)*
$($crate::required_check!($required_or_optional $identifier $field_name);)*

Ok(())
}

fn write<T: CompactThriftOutput>(&self, output: &mut T) -> Result<(), ThriftError> {
fn write<T: $crate::CompactThriftOutput>(&self, output: &mut T) -> std::result::Result<(), $crate::ThriftError> {
#[allow(unused_variables)]
#[allow(unused_mut)]
let mut last_field_id = 0_i16;
Expand All @@ -91,26 +88,26 @@ macro_rules! thrift_union {
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub enum $identifier {
$($(#[cfg_attr(not(doctest), $($field_attrss)*)])* $field_name(field_type!($field_type $($element_type)?))),*
$($(#[cfg_attr(not(doctest), $($field_attrss)*)])* $field_name($crate::field_type!($field_type $($element_type)?))),*
}

impl Default for $identifier {
fn default() -> Self {
union_default!($($field_name;)*)
$crate::union_default!($($field_name;)*)
}
}

#[allow(non_snake_case)]
impl <'i> CompactThriftProtocol<'i> for $identifier {
impl <'i> $crate::CompactThriftProtocol<'i> for $identifier {
const FIELD_TYPE: u8 = 12;

#[inline(never)]
fn fill<T: CompactThriftInput<'i>>(&mut self, input: &mut T) -> Result<(), ThriftError> {
fn fill<T: $crate::CompactThriftInput<'i>>(&mut self, input: &mut T) -> std::result::Result<(), $crate::ThriftError> {
let mut last_field_id = 0_i16;
let field_type = input.read_field_header(&mut last_field_id)?;

if field_type == 0 {
return Err(ThriftError::InvalidType);
return Err($crate::ThriftError::InvalidType);
}

match last_field_id {
Expand All @@ -123,18 +120,18 @@ macro_rules! thrift_union {
}
}),*
_ => {
return Err(ThriftError::MissingField(concat!(stringify!($struct_name), "\0").into()))
return Err($crate::ThriftError::MissingField(concat!(stringify!($struct_name), "\0").into()))
}
}
let stop = input.read_byte()?;
if stop != 0 {
return Err(ThriftError::MissingStop)
return Err($crate::ThriftError::MissingStop)
}

Ok(())
}

fn write<T: CompactThriftOutput>(&self, output: &mut T) -> Result<(), ThriftError> {
fn write<T: $crate::CompactThriftOutput>(&self, output: &mut T) -> std::result::Result<(), $crate::ThriftError> {
let mut last_field_id = 0_i16;
match self {
$(Self::$field_name(inner) => inner.write_field(output, $field_id, &mut last_field_id)?),*
Expand Down Expand Up @@ -165,17 +162,17 @@ macro_rules! thrift_enum {
$(pub const $field_name: Self = Self($field_value);)*
}

impl <'i> CompactThriftProtocol<'i> for $identifier {
impl <'i> $crate::CompactThriftProtocol<'i> for $identifier {
const FIELD_TYPE: u8 = 5; // i32

#[inline]
fn fill<T: CompactThriftInput<'i>>(&mut self, input: &mut T) -> Result<(), ThriftError> {
fn fill<T: $crate::CompactThriftInput<'i>>(&mut self, input: &mut T) -> std::result::Result<(), $crate::ThriftError> {
self.0 = input.read_i32()?;
Ok(())
}

#[inline]
fn write<T: CompactThriftOutput>(&self, output: &mut T) -> Result<(), ThriftError> {
fn write<T: $crate::CompactThriftOutput>(&self, output: &mut T) -> std::result::Result<(), $crate::ThriftError> {
output.write_i32(self.0)
}
}
Expand All @@ -196,8 +193,8 @@ macro_rules! union_default {
#[doc(hidden)]
#[macro_export]
macro_rules! field_type {
(list $element_type:ident) => { Vec< field_type!($element_type) > };
(set $element_type:ident) => { Vec< field_type!($element_type) > };
(list $element_type:ident) => { Vec< $crate::field_type!($element_type) > };
(set $element_type:ident) => { Vec< $crate::field_type!($element_type) > };
(binary) => { Vec<u8> };
(string) => { String };
($field_type:ident) => { $field_type };
Expand Down Expand Up @@ -229,7 +226,7 @@ macro_rules! required_set {
macro_rules! required_check {
(required $struct_name:ident $field_name:ident) => {
if !$field_name {
return Err(ThriftError::MissingField(concat!(stringify!($struct_name), "::", stringify!($field_name), "\0").into()))
return Err($crate::ThriftError::MissingField(concat!(stringify!($struct_name), "::", stringify!($field_name), "\0").into()))
}
};
(optional $struct_name:ident $field_name:ident) => {};
Expand Down

0 comments on commit 1807f8b

Please sign in to comment.