Skip to content

Commit

Permalink
Support marking methods as abstract (davidcole1340#154)
Browse files Browse the repository at this point in the history
For classes that are registered with `#[php_impl]` this allows functions to be marked as abstract.
  • Loading branch information
joehoyle authored Sep 30, 2022
1 parent 5d1fda4 commit d4ef116
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/macros/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum ParsedAttribute {
},
Constructor,
This,
Abstract,
}

#[derive(Default, Debug, FromMeta)]
Expand Down Expand Up @@ -212,6 +213,7 @@ pub fn parse_attribute(attr: &Attribute) -> Result<Option<ParsedAttribute>> {
"public" => ParsedAttribute::Visibility(Visibility::Public),
"protected" => ParsedAttribute::Visibility(Visibility::Protected),
"private" => ParsedAttribute::Visibility(Visibility::Private),
"abstract_method" => ParsedAttribute::Abstract,
"rename" => {
let ident = if let Meta::List(list) = meta {
if let Some(NestedMeta::Lit(lit)) = list.nested.first() {
Expand Down
8 changes: 8 additions & 0 deletions crates/macros/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Method {
pub optional: Option<String>,
pub output: Option<(String, bool)>,
pub _static: bool,
pub _abstract: bool,
pub visibility: Visibility,
}

Expand Down Expand Up @@ -81,6 +82,7 @@ pub fn parser(
let mut visibility = Visibility::Public;
let mut as_prop = None;
let mut identifier = None;
let mut is_abstract = false;
let mut is_constructor = false;
let docs = get_docs(&input.attrs);

Expand All @@ -90,6 +92,7 @@ pub fn parser(
ParsedAttribute::Default(list) => defaults = list,
ParsedAttribute::Optional(name) => optional = Some(name),
ParsedAttribute::Visibility(vis) => visibility = vis,
ParsedAttribute::Abstract => is_abstract = true,
ParsedAttribute::Rename(ident) => identifier = Some(ident),
ParsedAttribute::Property { prop_name, ty } => {
if as_prop.is_some() {
Expand Down Expand Up @@ -211,6 +214,7 @@ pub fn parser(
optional,
output: get_return_type(struct_ty, &input.sig.output)?,
_static: matches!(method_type, MethodType::Static),
_abstract: is_abstract,
visibility,
};

Expand Down Expand Up @@ -447,6 +451,10 @@ impl Method {
flags.push(quote! { Static });
}

if self._abstract {
flags.push(quote! { Abstract });
}

flags
.iter()
.map(|flag| quote! { ::ext_php_rs::flags::MethodFlags::#flag })
Expand Down

0 comments on commit d4ef116

Please sign in to comment.