Skip to content

Commit d492a85

Browse files
committed
stash!
1 parent c6f7346 commit d492a85

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

examples/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ name = "type_driven"
5252
path = "type_driven.rs"
5353
crate-type = ["cdylib"]
5454

55+
[example.type_driven.lints.rust]
56+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(static_ref_mut)'] }
57+
5558
[features]
5659
default = ["export-modules", "ngx/vendored"]
5760
# Generate `ngx_modules` table with module exports

examples/type_driven.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
use std::ffi::CStr;
2-
use std::ptr::addr_of_mut;
32

43
use ngx::ffi::{ngx_conf_t, ngx_str_t};
5-
use ngx::http::{HttpMainConf, HttpModuleSkel};
4+
use ngx::http::HttpMainConf;
65
use ngx::module::{
76
CommandArgFlag, CommandArgFlagSet, CommandCallRule, CommandContextFlag, CommandContextFlagSet, CommandError,
87
};
9-
use ngx::{arg_flags, context_flags, ngx_string};
8+
use ngx::{arg_flags, context_flags, exhibit_modules, ngx_string};
109
use ngx::{
1110
http::{DefaultInit, DefaultMerge, HttpModule, NgxHttpModule, NgxHttpModuleCommands},
1211
module::{Command, NgxModuleCommandsBuilder},
1312
};
1413

1514
#[cfg(static_ref_mut)]
1615
use ngx::{exhibit_modules, http::NgxHttpModuleCommandsRefMut, util::StaticRefMut};
16+
#[cfg(not(static_ref_mut))]
17+
use std::ptr::addr_of_mut;
18+
19+
#[cfg(not(static_ref_mut))]
20+
use ngx::{
21+
http::HttpModuleSkel,
22+
module::{NgxModule, NgxModuleCtx},
23+
ngx_modules,
24+
};
25+
#[cfg(not(static_ref_mut))]
26+
use std::ptr::addr_of;
1727

1828
#[cfg(all(static_ref_mut, feature = "export-modules"))]
1929
exhibit_modules!(HttpModuleSkel<FooBarHttpModule>);
2030

31+
#[cfg(all(not(static_ref_mut), feature = "export-modules"))]
32+
exhibit_modules!(HttpModuleSkel<FooBarHttpModule> => &mut FOO_BAR_HTTP_MODULE);
33+
34+
#[cfg(not(static_ref_mut))]
35+
static mut FOO_BAR_HTTP_MODULE: NgxModule<HttpModuleSkel<FooBarHttpModule>> = unsafe {
36+
NgxModule::new_from_ptr(
37+
addr_of!(FOO_BAR_HTTP_MODULE_CTX),
38+
addr_of!(FOO_BAR_HTTP_MODULE_COMMANDS),
39+
)
40+
};
41+
#[cfg(not(static_ref_mut))]
42+
static mut FOO_BAR_HTTP_MODULE_CTX: NgxModuleCtx<HttpModuleSkel<FooBarHttpModule>> =
43+
unsafe { NgxModuleCtx::from_raw() };
44+
45+
#[cfg(not(static_ref_mut))]
46+
static mut FOO_BAR_HTTP_MODULE_COMMANDS: NgxHttpModuleCommands<FooBarHttpModule, { 1 + 1 }> =
47+
NgxModuleCommandsBuilder::new().add::<FooBarCommand>().build();
48+
2149
struct FooBarHttpModule;
2250
impl HttpModule for FooBarHttpModule {
2351
#[cfg(static_ref_mut)]

src/module.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,39 @@ macro_rules! exhibit_modules {
449449
};
450450
}
451451

452+
/// Exhibit modules exported by this library.
453+
///
454+
/// These are normally generated by the Nginx module system, but need to be
455+
/// defined when building modules outside of it.
456+
#[macro_export]
457+
#[cfg(not(static_ref_mut))]
458+
macro_rules! exhibit_modules {
459+
($( $mod:ty => $entity:expr),+) => {
460+
#[no_mangle]
461+
#[allow(non_upper_case_globals)]
462+
pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $( {let _:[$mod;0]; 1}+ )+ 1] =
463+
unsafe {$crate::module::__macro::NgxModulesBuilder::new()$(.add::<$mod>($entity))+ .build() };
464+
465+
#[no_mangle]
466+
#[allow(non_upper_case_globals)]
467+
pub static mut ngx_module_names: [*const $crate::module::__macro::c_char; $( {let _:[$mod;0]; 1}+ ) + 1] =
468+
$crate::module::__macro::NgxModuleNamesBuilder::new()$(.add::<$mod>())+ .build();
469+
470+
#[no_mangle]
471+
#[allow(non_upper_case_globals)]
472+
pub static mut ngx_module_order: [*const ::core::ffi::c_char; 1] = [
473+
$crate::module::__macro::null()
474+
];
475+
};
476+
}
477+
452478
#[doc(hidden)]
453-
#[cfg(static_ref_mut)]
454479
pub mod __macro {
455480
pub use core::{ffi::c_char, ptr::null};
456481

457-
use crate::{ffi::ngx_module_t, util::ConstArrayBuilder};
458-
459482
use super::Module;
483+
use super::NgxModule;
484+
use crate::{ffi::ngx_module_t, util::ConstArrayBuilder};
460485

461486
pub struct NgxModulesBuilder<const N: usize>(ConstArrayBuilder<*const ngx_module_t, N>);
462487
impl<const N: usize> Default for NgxModulesBuilder<N> {
@@ -469,10 +494,16 @@ pub mod __macro {
469494
pub const fn new() -> Self {
470495
Self(ConstArrayBuilder::new())
471496
}
497+
#[cfg(static_ref_mut)]
472498
pub const fn add<M: Module>(mut self) -> Self {
473499
self.0 = self.0.push(unsafe { &M::SELF.to_ref().0 as *const _ });
474500
self
475501
}
502+
#[cfg(not(static_ref_mut))]
503+
pub const unsafe fn add<M: Module>(mut self, module: &'static mut NgxModule<M>) -> Self {
504+
self.0 = self.0.push(&module.0 as *const _);
505+
self
506+
}
476507
pub const fn build(self) -> [*const ngx_module_t; N] {
477508
self.0.push(null()).build()
478509
}

0 commit comments

Comments
 (0)