Skip to content

Commit

Permalink
chore: fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
JyJyJcr committed Dec 31, 2024
1 parent e5166d8 commit bebb6c7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 24 deletions.
6 changes: 4 additions & 2 deletions examples/type_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::ptr::addr_of_mut;

use ngx::ffi::{ngx_conf_t, ngx_str_t};
use ngx::http::{HttpMainConf, HttpModuleSkel};
use ngx::module::{CommandArgFlag, CommandArgFlagSet, CommandCallRule, CommandContextFlag, CommandContextFlagSet};
use ngx::module::{
CommandArgFlag, CommandArgFlagSet, CommandCallRule, CommandContextFlag, CommandContextFlagSet, CommandError,
};
use ngx::{arg_flags, context_flags, exhibit_modules, ngx_string};
use ngx::{
http::{DefaultInit, DefaultMerge, HttpModule, NgxHttpModule, NgxHttpModuleCommands, NgxHttpModuleCommandsRefMut},
Expand Down Expand Up @@ -60,7 +62,7 @@ impl Command for FooBarCommand {

const ARG_FLAG: CommandArgFlagSet = arg_flags!(CommandArgFlag::Take1, CommandArgFlag::Take2);

fn handler(_cf: &mut ngx_conf_t, conf: &mut <Self::CallRule as CommandCallRule>::Conf) -> Result<(), ()> {
fn handler(_cf: &mut ngx_conf_t, conf: &mut <Self::CallRule as CommandCallRule>::Conf) -> Result<(), CommandError> {
*conf += 1;
Ok(())
}
Expand Down
71 changes: 57 additions & 14 deletions src/http/module_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ::core::marker::PhantomData;
use std::ffi::{c_char, c_void, CStr};
use std::ptr::{addr_of, null_mut};

use super::{Merge, Request};
use super::{Merge, MergeConfigError, Request};

/// Wrapper of `HttpModule` implementing `Module`.
pub struct HttpModuleSkel<M: HttpModule>(PhantomData<M>);
Expand All @@ -40,6 +40,12 @@ 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>>);
impl<M: HttpModule> Default for NgxHttpModule<M> {
fn default() -> Self {
Self::new()
}
}

impl<M: HttpModule> NgxHttpModule<M> {
/// Construct this type.
pub const fn new() -> Self {
Expand Down Expand Up @@ -91,7 +97,7 @@ pub trait HttpModule: Sized + 'static {
type SrvConfSetting: MergeConfSetting;
/// Type deligating `create_loc_conf` and `merge_loc_conf`, and specifying LocConf type.
type LocConfSetting: MergeConfSetting;
///
/// Context type of Http request bound with this module.
type Ctx;
}

Expand All @@ -100,6 +106,10 @@ pub trait ConfigurationDelegate {
/// Module level configuration.
fn configuration(cf: &mut ngx_conf_t) -> Result<(), Status>;
/// Unsafe `configuration` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn configuration_unsafe(cf: *mut ngx_conf_t) -> ngx_int_t {
Self::configuration(&mut *cf).err().unwrap_or(Status::NGX_OK).into()
}
Expand All @@ -114,13 +124,34 @@ impl ConfigurationDelegate for () {
const CONFIGURATION: Option<unsafe extern "C" fn(*mut ngx_conf_t) -> ngx_int_t> = None;
}

/// Error for creating conf.
#[derive(Debug)]
pub struct ConfCreateError;
/// Error for initing conf.
#[derive(Debug)]
pub struct ConfInitError;
/// Error for merging conf.
#[derive(Debug)]
pub struct ConfMergeError;
impl From<MergeConfigError> for ConfMergeError {
fn from(value: MergeConfigError) -> Self {
match value {
MergeConfigError::NoValue => ConfMergeError,
}
}
}

/// Delegete type of conf object level configuration with init.
pub trait InitConfSetting {
/// Conf type.
type Conf;
/// create conf object.
fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ()>;
fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ConfCreateError>;
/// Unsafe `create` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn create_unsafe(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
if let Ok(conf) = Self::create(&mut *cf) {
Expand All @@ -129,8 +160,12 @@ pub trait InitConfSetting {
null_mut()
}
/// init conf object.
fn init(cf: &mut ngx_conf_t, conf: &mut Self::Conf) -> Result<(), ()>;
fn init(cf: &mut ngx_conf_t, conf: &mut Self::Conf) -> Result<(), ConfInitError>;
/// Unsafe `init` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_conf_t` and `c_void` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn init_unsafe(cf: *mut ngx_conf_t, conf: *mut c_void) -> *mut c_char {
if Self::init(&mut *cf, &mut *(conf as *mut _)).is_ok() {
return null_mut();
Expand All @@ -149,11 +184,11 @@ pub struct DefaultInit<C: Default>(PhantomData<C>);
impl<C: Default> InitConfSetting for DefaultInit<C> {
type Conf = C;

fn create(_cf: &mut ngx_conf_t) -> Result<Self::Conf, ()> {
fn create(_cf: &mut ngx_conf_t) -> Result<Self::Conf, ConfCreateError> {
Ok(Default::default())
}

fn init(_cf: &mut ngx_conf_t, _conf: &mut Self::Conf) -> Result<(), ()> {
fn init(_cf: &mut ngx_conf_t, _conf: &mut Self::Conf) -> Result<(), ConfInitError> {
Ok(())
}
}
Expand All @@ -163,8 +198,12 @@ pub trait MergeConfSetting {
/// Conf type.
type Conf;
/// create conf object.
fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ()>;
fn create(cf: &mut ngx_conf_t) -> Result<Self::Conf, ConfCreateError>;
/// Unsafe `create` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_conf_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn create_unsafe(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
if let Ok(conf) = Self::create(&mut *cf) {
Expand All @@ -173,8 +212,12 @@ pub trait MergeConfSetting {
null_mut()
}
/// merge conf objects.
fn merge(cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ()>;
fn merge(cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ConfMergeError>;
/// Unsafe `merge` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_conf_t` and `c_void` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn merge_unsafe(cf: *mut ngx_conf_t, prev: *mut c_void, conf: *mut c_void) -> *mut c_char {
if Self::merge(&mut *cf, &mut *(prev as *mut _), &mut *(conf as *mut _)).is_ok() {
return null_mut();
Expand All @@ -193,12 +236,12 @@ pub struct DefaultMerge<C: Default + Merge>(PhantomData<C>);
impl<C: Default + Merge> MergeConfSetting for DefaultMerge<C> {
type Conf = C;

fn create(_cf: &mut ngx_conf_t) -> Result<Self::Conf, ()> {
fn create(_cf: &mut ngx_conf_t) -> Result<Self::Conf, ConfCreateError> {
Ok(Default::default())
}

fn merge(_cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ()> {
conf.merge(prev).map_err(|_| ())
fn merge(_cf: &mut ngx_conf_t, prev: &mut Self::Conf, conf: &mut Self::Conf) -> Result<(), ConfMergeError> {
conf.merge(prev).map_err(|e| e.into())
}
}

Expand Down Expand Up @@ -263,7 +306,7 @@ pub enum Phase {
Log,
}
impl Phase {
const fn to_ngx_http_phases(self) -> ngx_http_phases {
const fn into_ngx_http_phases(self) -> ngx_http_phases {
use Phase::*;
match self {
PostRead => ngx_http_phases_NGX_HTTP_POST_READ_PHASE,
Expand Down Expand Up @@ -292,7 +335,7 @@ impl SetHttpHandler for ngx_conf_t {
unsafe { crate::http::ngx_http_conf_get_module_main_conf(self, &*addr_of!(ngx_http_core_module)).as_mut() }
.ok_or(Status::NGX_ERROR)?;
let pointer = unsafe {
(ngx_array_push(&mut conf.phases[H::PHASE.to_ngx_http_phases() as usize].handlers)
(ngx_array_push(&mut conf.phases[H::PHASE.into_ngx_http_phases() as usize].handlers)
as *mut ngx_http_handler_pt)
.as_mut()
}
Expand All @@ -312,5 +355,5 @@ pub trait HttpHandler {

unsafe extern "C" fn handle_func<H: HttpHandler>(request: *mut ngx_http_request_t) -> ngx_int_t {
let req = Request::from_ngx_http_request(request);
return H::handle(req).into();
H::handle(req).into()
}
52 changes: 46 additions & 6 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ 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>);
impl<M: Module> Default for NgxModule<M> {
fn default() -> Self {
Self::new()
}
}

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

init_master: M::MasterInitializer::INIT,
Expand Down Expand Up @@ -116,6 +122,8 @@ impl ModuleSignature {
pub struct NgxModuleCtx<M: Module>(M::Ctx);
impl<M: Module> NgxModuleCtx<M> {
/// Construct this type from raw ctx value.
/// # Safety
/// Callers should provide proper Context object consistent with `M:Module`.
pub const unsafe fn from_raw(inner: M::Ctx) -> Self {
Self(inner)
}
Expand All @@ -140,6 +148,12 @@ pub struct NgxModuleCommands<M: Module, const N: usize>([ngx_command_t; N], Phan

/// `NgxModuleCommands` builder.
pub struct NgxModuleCommandsBuilder<M: Module, const N: usize>(ConstArrayBuilder<ngx_command_t, N>, PhantomData<M>);
impl<M: Module, const N: usize> Default for NgxModuleCommandsBuilder<M, N> {
fn default() -> Self {
Self::new()
}
}

impl<M: Module, const N: usize> NgxModuleCommandsBuilder<M, N> {
/// Constructs new, empty builder with capacity size = N..
pub const fn new() -> Self {
Expand All @@ -159,6 +173,10 @@ impl<M: Module, const N: usize> NgxModuleCommandsBuilder<M, N> {
}
}

/// Command derivative handler error.
#[derive(Debug)]
pub struct CommandError;

/// Type safe command interface.
pub trait Command {
/// Type expressing call rule.
Expand All @@ -170,7 +188,7 @@ pub trait Command {
/// Arg Flags.
const ARG_FLAG: CommandArgFlagSet;
/// handle command directive
fn handler(cf: &mut ngx_conf_t, conf: &mut <Self::CallRule as CommandCallRule>::Conf) -> Result<(), ()>;
fn handler(cf: &mut ngx_conf_t, conf: &mut <Self::CallRule as CommandCallRule>::Conf) -> Result<(), CommandError>;
}

/// Command call interface containing information for proper call.
Expand All @@ -195,7 +213,7 @@ impl CommandOffset {
pub const unsafe fn from_ngx_uint(offset: ngx_uint_t) -> Self {
Self(offset)
}
const fn to_ngx_uint(self) -> ngx_uint_t {
const fn into_ngx_uint(self) -> ngx_uint_t {
self.0
}
}
Expand Down Expand Up @@ -288,14 +306,14 @@ where
ngx_command_t {
name: C::NAME,
type_: C::CONTEXT_FLAG.to_ngx_uint() | C::ARG_FLAG.to_ngx_uint(),
set: Some(command_handler::<M, C>),
conf: <C::CallRule as CommandCallRuleBy<M>>::OFFSET.to_ngx_uint(),
set: Some(command_handler::<C>),
conf: <C::CallRule as CommandCallRuleBy<M>>::OFFSET.into_ngx_uint(),
offset: 0,
post: null_mut(),
}
}

unsafe extern "C" fn command_handler<M: Module, C: Command>(
unsafe extern "C" fn command_handler<C: Command>(
cf: *mut ngx_conf_t,
_cmd: *mut ngx_command_t,
conf: *mut c_void,
Expand All @@ -312,6 +330,9 @@ pub trait PreCycleDelegate {
/// Initialize in pre-cycle.
fn init(log: &mut ngx_log_t) -> ngx_int_t;
/// Unsafe `init` wrapper for pointer usage.
/// # Safety
/// Callers should provide valid non-null `ngx_log_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn init_unsafe(log: *mut ngx_log_t) -> ngx_int_t {
Self::init(&mut *log)
}
Expand All @@ -332,10 +353,17 @@ pub trait CycleDelegate {
/// finalize in cycle end time.
fn exit(cycle: &mut ngx_cycle_t);
/// Unsafe `init` wrapper for pointer usage.
/// # Safety
/// Callers should provide valid non-null `ngx_cycle_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn init_unsafe(cycle: *mut ngx_cycle_t) -> ngx_int_t {
Self::init(&mut *cycle)
}
/// Unsafe `exit` wrapper for pointer usage.
///
/// # Safety
/// Callers should provide valid non-null `ngx_cycle_t` arguments. Implementers must
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn exit_unsafe(cycle: *mut ngx_cycle_t) {
Self::exit(&mut *cycle)
}
Expand Down Expand Up @@ -390,6 +418,12 @@ pub mod __macro {
use super::Module;

pub struct NgxModulesBuilder<const N: usize>(ConstArrayBuilder<*const ngx_module_t, N>);
impl<const N: usize> Default for NgxModulesBuilder<N> {
fn default() -> Self {
Self::new()
}
}

impl<const N: usize> NgxModulesBuilder<N> {
pub const fn new() -> Self {
Self(ConstArrayBuilder::new())
Expand All @@ -403,6 +437,12 @@ pub mod __macro {
}
}
pub struct NgxModuleNamesBuilder<const N: usize>(ConstArrayBuilder<*const c_char, N>);
impl<const N: usize> Default for NgxModuleNamesBuilder<N> {
fn default() -> Self {
Self::new()
}
}

impl<const N: usize> NgxModuleNamesBuilder<N> {
pub const fn new() -> Self {
Self(ConstArrayBuilder::new())
Expand Down
6 changes: 6 additions & 0 deletions src/util/const_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub struct ConstArrayBuilder<T, const N: usize> {
len: usize,
inner: [MaybeUninit<T>; N],
}
impl<T, const N: usize> Default for ConstArrayBuilder<T, N> {
fn default() -> Self {
Self::new()
}
}

impl<T, const N: usize> ConstArrayBuilder<T, N> {
/// Constructs a new, empty builder with capacity size = N.
pub const fn new() -> Self {
Expand Down
10 changes: 8 additions & 2 deletions src/util/static_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ impl<E: 'static + ?Sized> StaticRefMut<E> {
Self(unsafe { NonNull::new_unchecked(e) })
}
/// Converts this type into a static immutable reference.
///
/// # Safety
/// This sunction is marked unsafe because provide a reference to static mutable
pub const unsafe fn to_ref(self) -> &'static E {
unsafe { self.0.as_ref() }
}
/// Converts this type into a static mutable reference.
///
/// # Safety
/// This sunction is marked unsafe because provide a reference to static mutable
pub const unsafe fn to_mut(mut self) -> &'static mut E {
self.0.as_mut()
}
Expand Down Expand Up @@ -104,8 +110,8 @@ mod test {

// mutable static access is always unsafe.
let a_value = unsafe { B }; // actually this is Copy trait
let a_ref: &_ = unsafe { &*&raw const B };
let a_mut: &mut _ = unsafe { &mut *&raw mut B };
let a_ref: &_ = unsafe { &*addr_of!(B) };
let a_mut: &mut _ = unsafe { &mut *addr_of_mut!(B) };
// StaticRefMut follow the rule.
let a_ref_from_mut: &_ = unsafe { B_MUT.to_ref() };
let a_mut_from_mut: &mut _ = unsafe { B_MUT.to_mut() };
Expand Down

0 comments on commit bebb6c7

Please sign in to comment.