Skip to content

Commit

Permalink
fix: 1.79.0 compat cfg out
Browse files Browse the repository at this point in the history
  • Loading branch information
JyJyJcr committed Dec 31, 2024
1 parent bebb6c7 commit 1b3c961
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ default = ["vendored"]
# This could be disabled with `--no-default-features` to minimize the dependency tree
# when building against an existing copy of the NGINX with the NGX_OBJS variable.
vendored = ["nginx-sys/vendored"]
static_ref = []

[badges]
maintenance = { status = "experimental" }
Expand Down
2 changes: 2 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ crate-type = ["cdylib"]
name = "type_driven"
path = "type_driven.rs"
crate-type = ["cdylib"]
required-features = ["static_ref"]

[features]
default = ["export-modules", "ngx/vendored"]
Expand All @@ -61,3 +62,4 @@ default = ["export-modules", "ngx/vendored"]
# See https://github.com/rust-lang/rust/issues/20267
export-modules = []
linux = []
static_ref = ["ngx/static_ref"]
43 changes: 40 additions & 3 deletions src/http/module_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use crate::core::{Pool, Status, NGX_CONF_ERROR};
use crate::ffi::{ngx_http_module_t, NGX_HTTP_MODULE};
use crate::module::{
CommandCallRule, CommandCallRuleBy, CommandOffset, CycleDelegate, Module, ModuleSignature, NgxModule,
NgxModuleCommands, NgxModuleCommandsRefMut, NgxModuleCtx, PreCycleDelegate,
NgxModuleCommands, NgxModuleCtx, PreCycleDelegate,
};
use crate::util::StaticRefMut;
#[cfg(feature = "static_ref")]
use crate::{module::NgxModuleCommandsRefMut, util::StaticRefMut};
use ::core::marker::PhantomData;
use std::ffi::{c_char, c_void, CStr};
use std::ptr::{addr_of, null_mut};
Expand All @@ -25,11 +26,14 @@ use super::{Merge, MergeConfigError, Request};
/// Wrapper of `HttpModule` implementing `Module`.
pub struct HttpModuleSkel<M: HttpModule>(PhantomData<M>);
impl<M: HttpModule> Module for HttpModuleSkel<M> {
#[cfg(feature = "static_ref")]
const SELF: StaticRefMut<NgxModule<Self>> = unsafe { StaticRefMut::from_mut(&mut M::SELF.to_mut().0) };
const NAME: &'static CStr = M::NAME;
const TYPE: ModuleSignature = unsafe { ModuleSignature::from_ngx_uint(NGX_HTTP_MODULE as ngx_uint_t) };
type Ctx = ngx_http_module_t;
#[cfg(feature = "static_ref")]
const CTX: StaticRefMut<NgxModuleCtx<Self>> = unsafe { StaticRefMut::from_mut(&mut M::SELF.to_mut().1) };
#[cfg(feature = "static_ref")]
const COMMANDS: NgxModuleCommandsRefMut<Self> = M::COMMANDS;

type MasterInitializer = M::MasterInitializer;
Expand All @@ -39,7 +43,11 @@ impl<M: HttpModule> Module for HttpModuleSkel<M> {
}

/// Type safe wrapper of `ngx_module_t` and `ngx_http_module_t` by specifying `Module`.
pub struct NgxHttpModule<M: HttpModule>(NgxModule<HttpModuleSkel<M>>, NgxModuleCtx<HttpModuleSkel<M>>);
pub struct NgxHttpModule<M: HttpModule>(
#[allow(dead_code)] NgxModule<HttpModuleSkel<M>>,
NgxModuleCtx<HttpModuleSkel<M>>,
);
#[cfg(feature = "static_ref")]
impl<M: HttpModule> Default for NgxHttpModule<M> {
fn default() -> Self {
Self::new()
Expand All @@ -48,6 +56,7 @@ impl<M: HttpModule> Default for NgxHttpModule<M> {

impl<M: HttpModule> NgxHttpModule<M> {
/// Construct this type.
#[cfg(feature = "static_ref")]
pub const fn new() -> Self {
Self(NgxModule::new(), unsafe {
NgxModuleCtx::from_raw(ngx_http_module_t {
Expand All @@ -62,19 +71,47 @@ impl<M: HttpModule> NgxHttpModule<M> {
})
})
}

/// Construct this type from immutable pointer.
///
/// # Safety
/// Callers must ensure that provided pointers are to static mutables.
pub const unsafe fn new_from_ptr<const N: usize>(
this: *const NgxHttpModule<M>,
commands: *const NgxHttpModuleCommands<M, N>,
) -> Self {
Self(
unsafe { NgxModule::new_from_ptr(&(*this).1 as *const _, commands) },
unsafe {
NgxModuleCtx::from_raw(ngx_http_module_t {
preconfiguration: M::PreConfiguration::CONFIGURATION,
postconfiguration: M::PostConfiguration::CONFIGURATION,
create_main_conf: M::MainConfSetting::CREATE,
init_main_conf: M::MainConfSetting::INIT,
create_srv_conf: M::SrvConfSetting::CREATE,
merge_srv_conf: M::SrvConfSetting::MERGE,
create_loc_conf: M::LocConfSetting::CREATE,
merge_loc_conf: M::LocConfSetting::MERGE,
})
},
)
}
}
/// Type Alias of `NgxModuleCommands` for `HttpModule`.
pub type NgxHttpModuleCommands<M, const N: usize> = NgxModuleCommands<HttpModuleSkel<M>, N>;
/// Type Alias of `NgxModuleCommandsRefMut` for `HttpModule`.
#[cfg(feature = "static_ref")]
pub type NgxHttpModuleCommandsRefMut<M> = NgxModuleCommandsRefMut<HttpModuleSkel<M>>;

/// Type safe interface expressing unique Nginx Http module.
pub trait HttpModule: Sized + 'static {
/// Wrapper of static mutable `NgxModule` and `NgxModuleCtx` object expressing this module.
#[cfg(feature = "static_ref")]
const SELF: StaticRefMut<NgxHttpModule<Self>>;
/// CStr module name expression.
const NAME: &'static CStr;
/// Wrapper of static mutable `NgxHttpModuleCommands` object bound to this module.
#[cfg(feature = "static_ref")]
const COMMANDS: NgxHttpModuleCommandsRefMut<Self>;

/// Type deligating `init_master` (not called now).
Expand Down
49 changes: 45 additions & 4 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use crate::{
ngx_uint_t, NGX_RS_MODULE_SIGNATURE,
},
ngx_null_command,
util::{ConstArrayBuilder, StaticRefMut},
util::ConstArrayBuilder,
};

#[cfg(feature = "static_ref")]
use crate::util::StaticRefMut;

use ::core::{
ffi::{c_char, c_void, CStr},
marker::PhantomData,
Expand Down Expand Up @@ -48,6 +52,7 @@ pub const NGX_MODULE_EMPTY: ngx_module_t = ngx_module_t {
/// Type safe interface expressing unique Nginx module.
pub trait Module: Sized + 'static {
/// Wrapper of static mutable `NgxModule` object expressing this module.
#[cfg(feature = "static_ref")]
const SELF: StaticRefMut<NgxModule<Self>>;
/// CStr module name expression.
const NAME: &'static CStr;
Expand All @@ -56,8 +61,10 @@ pub trait Module: Sized + 'static {
/// Module context type.
type Ctx: 'static;
/// Wrapper of static mutable `NgxModuleCtx` object bound to this module as context object.
#[cfg(feature = "static_ref")]
const CTX: StaticRefMut<NgxModuleCtx<Self>>;
/// Wrapper of static mutable `NgxModuleCommands` object bound to this module.
#[cfg(feature = "static_ref")]
const COMMANDS: NgxModuleCommandsRefMut<Self>;

/// Type deligating `init_master` (not called now).
Expand All @@ -71,7 +78,8 @@ pub trait Module: Sized + 'static {
}

/// Type safe wrapper of `ngx_module_t` by specifying `Module`.
pub struct NgxModule<M: Module>(ngx_module_t, PhantomData<M>);
pub struct NgxModule<M: Module>(#[allow(dead_code)] ngx_module_t, PhantomData<M>);
#[cfg(feature = "static_ref")]
impl<M: Module> Default for NgxModule<M> {
fn default() -> Self {
Self::new()
Expand All @@ -80,11 +88,40 @@ impl<M: Module> Default for NgxModule<M> {

impl<M: Module> NgxModule<M> {
/// Construct this type.
#[cfg(feature = "static_ref")]
pub const fn new() -> Self {
Self(
ngx_module_t {
ctx: M::CTX.to_mut_ptr() as *mut _,
commands: &raw mut unsafe { M::COMMANDS.1.to_mut() }[0],
commands: &mut unsafe { M::COMMANDS.1.to_mut() }[0] as *mut _,
type_: M::TYPE.to_ngx_uint(),

init_master: M::MasterInitializer::INIT,
init_module: M::ModuleDelegate::INIT,
init_process: M::ProcessDelegate::INIT,
init_thread: M::ThreadDelegate::INIT,
exit_thread: M::ThreadDelegate::EXIT,
exit_process: M::ProcessDelegate::EXIT,
exit_master: M::ModuleDelegate::EXIT,

..NGX_MODULE_EMPTY
},
PhantomData,
)
}

/// Construct this type from immutable pointer.
///
/// # Safety
/// Callers must ensure that provided pointers are to static mutables.
pub const unsafe fn new_from_ptr<const N: usize>(
ctx: *const NgxModuleCtx<M>,
commands: *const NgxModuleCommands<M, N>,
) -> Self {
Self(
ngx_module_t {
ctx: unsafe { &(*ctx).0 } as *const _ as *mut _,
commands: unsafe { &(*commands).0[0] } as *const ngx_command_t as *mut ngx_command_t,
type_: M::TYPE.to_ngx_uint(),

init_master: M::MasterInitializer::INIT,
Expand Down Expand Up @@ -130,7 +167,9 @@ impl<M: Module> NgxModuleCtx<M> {
}

/// Reference to static mutable `NgxModuleCommands` object ignoring the length.
#[cfg(feature = "static_ref")]
pub struct NgxModuleCommandsRefMut<M: Module>(PhantomData<M>, StaticRefMut<[ngx_command_t]>);
#[cfg(feature = "static_ref")]
impl<M: Module> NgxModuleCommandsRefMut<M> {
/// Wrap a static mutable reference to `NgxModuleCommands` into this type.
///
Expand Down Expand Up @@ -389,6 +428,7 @@ impl CycleDelegate for () {
/// These are normally generated by the Nginx module system, but need to be
/// defined when building modules outside of it.
#[macro_export]
#[cfg(feature = "static_ref")]
macro_rules! exhibit_modules {
($( $mod:ty ),+) => {
#[no_mangle]
Expand All @@ -410,6 +450,7 @@ macro_rules! exhibit_modules {
}

#[doc(hidden)]
#[cfg(feature = "static_ref")]
pub mod __macro {
pub use core::{ffi::c_char, ptr::null};

Expand All @@ -429,7 +470,7 @@ pub mod __macro {
Self(ConstArrayBuilder::new())
}
pub const fn add<M: Module>(mut self) -> Self {
self.0 = self.0.push(unsafe { &raw const M::SELF.to_ref().0 });
self.0 = self.0.push(unsafe { &M::SELF.to_ref().0 as *const _ });
self
}
pub const fn build(self) -> [*const ngx_module_t; N] {
Expand Down
2 changes: 2 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod const_array;
#[cfg(feature = "static_ref")]
mod static_ref;

pub use const_array::*;
#[cfg(feature = "static_ref")]
pub use static_ref::*;

0 comments on commit 1b3c961

Please sign in to comment.