Skip to content

Commit

Permalink
Refactor macro for registering submodules
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1672
  • Loading branch information
treiher committed Aug 29, 2024
1 parent a2dc745 commit 120d701
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 62 deletions.
4 changes: 2 additions & 2 deletions rapidflux/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pyo3::{

use librapidflux as lib;

use crate::register_submodule_functions;
use crate::register_submodule_declarations;

/// Format a Python's string with the arguments passed in `args` dict.
///
Expand Down Expand Up @@ -111,4 +111,4 @@ fn set_quiet(value: bool) {
lib::diagnostics::logging::set_quiet(value);
}

register_submodule_functions!(logging, [error, help, info, note, set_quiet, warning]);
register_submodule_declarations!(logging, [], [error, help, info, note, set_quiet, warning]);
4 changes: 2 additions & 2 deletions rapidflux/src/source_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use librapidflux::source_code as lib;
use pyo3::prelude::*;

use crate::register_submodule_functions;
use crate::register_submodule_declarations;

#[pyfunction]
fn register(path: PathBuf, source_code: String) {
Expand All @@ -26,4 +26,4 @@ fn clear() {
lib::clear();
}

register_submodule_functions!(source_code, [clear, register, retrieve]);
register_submodule_declarations!(source_code, [], [clear, register, retrieve]);
21 changes: 4 additions & 17 deletions rapidflux/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use librapidflux::ty as lib;
use crate::{
diagnostics::Location,
identifier::{to_id, ID},
impl_states, register_submodule_classes,
impl_states, register_submodule_declarations,
};

#[pyclass(subclass, module = "rflx.rapidflux.ty")]
Expand Down Expand Up @@ -177,18 +177,6 @@ impl Any {
}
}

// #[pyclass(extends=Any, subclass, module = "rflx.rapidflux.ty")]
// #[derive(Clone, Serialize, Deserialize)]
// pub struct NamedType;

// #[pymethods]
// impl NamedType {
// #[new]
// fn new() -> PyClassInitializer<Self> {
// PyClassInitializer::from(Any::new()).add_subclass(Self)
// }
// }

#[pyclass(extends=Any, module = "rflx.rapidflux.ty")]
#[derive(Clone, Serialize, Deserialize)]
pub struct Enumeration(lib::Enumeration);
Expand Down Expand Up @@ -1139,7 +1127,6 @@ impl_states!(
Type,
Undefined,
Any,
// NamedType,
Enumeration,
AnyInteger,
UniversalInteger,
Expand All @@ -1153,15 +1140,14 @@ impl_states!(
Message,
Channel
);
register_submodule_classes!(
register_submodule_declarations!(
ty,
[
Bounds,
Builtins,
Type,
Undefined,
Any,
// NamedType,
Enumeration,
AnyInteger,
UniversalInteger,
Expand All @@ -1174,5 +1160,6 @@ register_submodule_classes!(
Structure,
Message,
Channel,
]
],
[]
);
50 changes: 9 additions & 41 deletions rapidflux/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_states {
};
}

/// Register classes in a submodule.
/// Register classes and functions in a submodule.
///
/// This macro generate a `register_<module name>_module` function that is used by the
/// `register_submodule` function later to add a submodule in `rapidflux`.
Expand All @@ -43,15 +43,18 @@ macro_rules! impl_states {
/// use pyo3::prelude::*;
///
/// #[pyclass]
/// pub struct Foo;
/// pub struct A;
/// #[pyclass]
/// pub struct Bar;
/// pub struct B;
///
/// register_submodule_classes!(foo, [Foo, Bar]);
/// fn bar() {}
/// fn baz() {}
///
/// register_submodule_declarations!(foo, [A, B], [bar, baz]);
/// ```
#[macro_export]
macro_rules! register_submodule_classes {
($module_name:ident, [$($class_name:ident),+ $(,)?] $(,)?) => {
macro_rules! register_submodule_declarations {
($module_name:ident, [$($class_name:ident),* $(,)?], [$($fn_name:ident),* $(,)?] $(,)?) => {
::paste::paste! {
pub fn [<register_ $module_name _module>]<'py>(
py: Python<'py>,
Expand All @@ -63,41 +66,6 @@ macro_rules! register_submodule_classes {
m.add_class::<$class_name>()?;
)*

// Submodules need to be added manually to `sys.modules`.
// See: https://github.com/PyO3/pyo3/issues/759#issuecomment-1208179322
py.import_bound("sys")?
.getattr("modules")?
.set_item(PY_MODULE_PATH, m)
}
}
};
}

/// Register functions in a submodule.
///
/// Works like `register_submodule_classes` but takes one or more functions instead.
///
/// # Examples
///
/// ```rust
/// // in `foo.rs`
/// use pyo3::prelude::*;
///
/// fn bar() {}
/// fn baz() {}
///
/// register_submodule_functions!(foo, [bar, baz]);
/// ```
#[macro_export]
macro_rules! register_submodule_functions {
($module_name:ident, [$($fn_name:ident),+ $(,)?] $(,)?) => {
::paste::paste! {
pub fn [<register_ $module_name _module>]<'py>(
py: Python<'py>,
m: &Bound<'py, PyModule>
) -> PyResult<()> {
const PY_MODULE_PATH: &str = concat!("rflx.rapidflux.", stringify!($module_name));

$(
m.add_function(wrap_pyfunction!($fn_name, m)?)?;
)*
Expand Down

0 comments on commit 120d701

Please sign in to comment.