Skip to content

Commit

Permalink
fix: cfg out by original flag
Browse files Browse the repository at this point in the history
  • Loading branch information
JyJyJcr committed Dec 31, 2024
1 parent 1b3c961 commit c6f7346
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ 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" }

[dev-dependencies]
target-triple = "0.1.2"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(static_ref_mut)'] }
2 changes: 0 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ 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 @@ -62,4 +61,3 @@ default = ["export-modules", "ngx/vendored"]
# See https://github.com/rust-lang/rust/issues/20267
export-modules = []
linux = []
static_ref = ["ngx/static_ref"]
12 changes: 8 additions & 4 deletions examples/type_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ use ngx::http::{HttpMainConf, HttpModuleSkel};
use ngx::module::{
CommandArgFlag, CommandArgFlagSet, CommandCallRule, CommandContextFlag, CommandContextFlagSet, CommandError,
};
use ngx::{arg_flags, context_flags, exhibit_modules, ngx_string};
use ngx::{arg_flags, context_flags, ngx_string};
use ngx::{
http::{DefaultInit, DefaultMerge, HttpModule, NgxHttpModule, NgxHttpModuleCommands, NgxHttpModuleCommandsRefMut},
http::{DefaultInit, DefaultMerge, HttpModule, NgxHttpModule, NgxHttpModuleCommands},
module::{Command, NgxModuleCommandsBuilder},
util::StaticRefMut,
};

#[cfg(feature = "export-modules")]
#[cfg(static_ref_mut)]
use ngx::{exhibit_modules, http::NgxHttpModuleCommandsRefMut, util::StaticRefMut};

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

struct FooBarHttpModule;
impl HttpModule for FooBarHttpModule {
#[cfg(static_ref_mut)]
const SELF: StaticRefMut<NgxHttpModule<Self>> = {
static mut FOO_BAR_HTTP_MODULE: NgxHttpModule<FooBarHttpModule> = NgxHttpModule::new();
unsafe { StaticRefMut::from_mut(&mut *addr_of_mut!(FOO_BAR_HTTP_MODULE)) }
};

const NAME: &'static CStr = c"foo_bar_module";

#[cfg(static_ref_mut)]
const COMMANDS: NgxHttpModuleCommandsRefMut<Self> = {
static mut FOO_BAR_HTTP_MODULE_COMMANDS: NgxHttpModuleCommands<FooBarHttpModule, { 1 + 1 }> =
NgxModuleCommandsBuilder::new().add::<FooBarCommand>().build();
Expand Down
18 changes: 9 additions & 9 deletions src/http/module_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::module::{
CommandCallRule, CommandCallRuleBy, CommandOffset, CycleDelegate, Module, ModuleSignature, NgxModule,
NgxModuleCommands, NgxModuleCtx, PreCycleDelegate,
};
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
use crate::{module::NgxModuleCommandsRefMut, util::StaticRefMut};
use ::core::marker::PhantomData;
use std::ffi::{c_char, c_void, CStr};
Expand All @@ -26,14 +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")]
#[cfg(static_ref_mut)]
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")]
#[cfg(static_ref_mut)]
const CTX: StaticRefMut<NgxModuleCtx<Self>> = unsafe { StaticRefMut::from_mut(&mut M::SELF.to_mut().1) };
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
const COMMANDS: NgxModuleCommandsRefMut<Self> = M::COMMANDS;

type MasterInitializer = M::MasterInitializer;
Expand All @@ -47,7 +47,7 @@ pub struct NgxHttpModule<M: HttpModule>(
#[allow(dead_code)] NgxModule<HttpModuleSkel<M>>,
NgxModuleCtx<HttpModuleSkel<M>>,
);
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
impl<M: HttpModule> Default for NgxHttpModule<M> {
fn default() -> Self {
Self::new()
Expand All @@ -56,7 +56,7 @@ impl<M: HttpModule> Default for NgxHttpModule<M> {

impl<M: HttpModule> NgxHttpModule<M> {
/// Construct this type.
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
pub const fn new() -> Self {
Self(NgxModule::new(), unsafe {
NgxModuleCtx::from_raw(ngx_http_module_t {
Expand Down Expand Up @@ -100,18 +100,18 @@ impl<M: HttpModule> NgxHttpModule<M> {
/// 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")]
#[cfg(static_ref_mut)]
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")]
#[cfg(static_ref_mut)]
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")]
#[cfg(static_ref_mut)]
const COMMANDS: NgxHttpModuleCommandsRefMut<Self>;

/// Type deligating `init_master` (not called now).
Expand Down
20 changes: 10 additions & 10 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
util::ConstArrayBuilder,
};

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

use ::core::{
Expand Down Expand Up @@ -52,7 +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")]
#[cfg(static_ref_mut)]
const SELF: StaticRefMut<NgxModule<Self>>;
/// CStr module name expression.
const NAME: &'static CStr;
Expand All @@ -61,10 +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")]
#[cfg(static_ref_mut)]
const CTX: StaticRefMut<NgxModuleCtx<Self>>;
/// Wrapper of static mutable `NgxModuleCommands` object bound to this module.
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
const COMMANDS: NgxModuleCommandsRefMut<Self>;

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

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

impl<M: Module> NgxModule<M> {
/// Construct this type.
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
pub const fn new() -> Self {
Self(
ngx_module_t {
Expand Down Expand Up @@ -167,9 +167,9 @@ impl<M: Module> NgxModuleCtx<M> {
}

/// Reference to static mutable `NgxModuleCommands` object ignoring the length.
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
pub struct NgxModuleCommandsRefMut<M: Module>(PhantomData<M>, StaticRefMut<[ngx_command_t]>);
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
impl<M: Module> NgxModuleCommandsRefMut<M> {
/// Wrap a static mutable reference to `NgxModuleCommands` into this type.
///
Expand Down Expand Up @@ -428,7 +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")]
#[cfg(static_ref_mut)]
macro_rules! exhibit_modules {
($( $mod:ty ),+) => {
#[no_mangle]
Expand All @@ -450,7 +450,7 @@ macro_rules! exhibit_modules {
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod const_array;
#[cfg(feature = "static_ref")]
#[cfg(static_ref_mut)]
mod static_ref;

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

0 comments on commit c6f7346

Please sign in to comment.