-
Notifications
You must be signed in to change notification settings - Fork 91
feat: add core main-conf access #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryan-surname-p
wants to merge
3
commits into
nginx:main
Choose a base branch
from
ryan-surname-p:rnpridgeon/core_conf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| use core::ptr::NonNull; | ||
|
|
||
| use crate::ffi::{ngx_core_conf_t, ngx_module_t}; | ||
|
|
||
| /// Raw access to core module main configuration slots. | ||
| /// | ||
| /// This trait is implemented for NGINX-owned types such as `ngx_cycle_t` and `ngx_conf_t` | ||
| /// that carry configuration context pointers. It exposes the low-level lookup step that | ||
| /// retrieves a module's main configuration as an untyped pointer. | ||
| /// | ||
| /// Most callers should not use this trait directly. Prefer `CoreModuleMainConf` to obtain a | ||
| /// typed reference for a specific core module. | ||
| pub trait CoreModuleConfExt { | ||
| /// Get a non-null pointer to a core module's main configuration. | ||
| /// | ||
| /// # Safety | ||
| /// Caller must ensure that type `T` matches the configuration type for the specified module. | ||
| /// Supplying the wrong type will produce an invalid typed pointer. | ||
| #[inline] | ||
| unsafe fn core_main_conf_unchecked<T>(&self, _module: &ngx_module_t) -> Option<NonNull<T>> { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| impl CoreModuleConfExt for crate::ffi::ngx_cycle_t { | ||
| #[inline] | ||
| unsafe fn core_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> { | ||
| let conf_ctx = NonNull::new(self.conf_ctx)?; | ||
| let conf = unsafe { *conf_ctx.as_ptr().add(module.index) }; | ||
| NonNull::new(conf.cast()) | ||
| } | ||
| } | ||
|
|
||
| impl CoreModuleConfExt for crate::ffi::ngx_conf_t { | ||
| #[inline] | ||
| unsafe fn core_main_conf_unchecked<T>(&self, module: &ngx_module_t) -> Option<NonNull<T>> { | ||
| unsafe { self.cycle.as_ref()?.core_main_conf_unchecked(module) } | ||
| } | ||
| } | ||
|
|
||
| /// Typed access to a core module's main configuration. | ||
| /// | ||
| /// Implement this trait for a concrete core-style module to associate it with its main | ||
| /// configuration type and global `ngx_module_t`. The provided default methods build on top of | ||
| /// `CoreModuleConfExt` to turn the raw configuration slot lookup into typed references. | ||
| /// | ||
| /// # Safety | ||
| /// Caller must ensure that type `CoreModuleMainConf::MainConf` matches the configuration type | ||
| /// for the specified module. | ||
| pub unsafe trait CoreModuleMainConf { | ||
| /// Concrete type of this module's main configuration. | ||
| type MainConf; | ||
|
|
||
| /// Returns the global `ngx_module_t` describing this module. | ||
| fn module() -> &'static ngx_module_t; | ||
|
|
||
| /// Get a typed shared reference to this module's main configuration. | ||
| fn main_conf(o: &impl CoreModuleConfExt) -> Option<&'static Self::MainConf> { | ||
| unsafe { Some(o.core_main_conf_unchecked(Self::module())?.as_ref()) } | ||
| } | ||
|
|
||
| /// Get a typed mutable reference to this module's main configuration. | ||
| fn main_conf_mut(o: &impl CoreModuleConfExt) -> Option<&'static mut Self::MainConf> { | ||
| unsafe { Some(o.core_main_conf_unchecked(Self::module())?.as_mut()) } | ||
| } | ||
| } | ||
|
|
||
| /// Auxiliary structure to access `ngx_core_module` configuration. | ||
| pub struct NgxCoreModule; | ||
|
|
||
| unsafe impl CoreModuleMainConf for NgxCoreModule { | ||
| type MainConf = ngx_core_conf_t; | ||
|
|
||
| fn module() -> &'static ngx_module_t { | ||
| unsafe { &*core::ptr::addr_of!(nginx_sys::ngx_core_module) } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| extern crate alloc; | ||
| extern crate std; | ||
|
|
||
| use alloc::boxed::Box; | ||
| use core::ffi::c_void; | ||
| use core::mem::MaybeUninit; | ||
|
|
||
| use super::{CoreModuleConfExt, CoreModuleMainConf}; | ||
| use crate::ffi::{ngx_conf_t, ngx_cycle_t, ngx_module_t}; | ||
|
|
||
| type CoreConfSlot = *mut *mut *mut c_void; | ||
|
|
||
| fn module_with_index(index: usize) -> ngx_module_t { | ||
| let mut module = ngx_module_t::default(); | ||
| module.index = index; | ||
| module | ||
| } | ||
|
|
||
| #[test] | ||
| fn null_conf_ctx_returns_none() { | ||
| let cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| let module = module_with_index(0); | ||
| assert!(unsafe { cycle.core_main_conf_unchecked::<u32>(&module) }.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn missing_module_slot_returns_none() { | ||
| let mut slots: [CoreConfSlot; 2] = [core::ptr::null_mut(); 2]; | ||
| let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| cycle.conf_ctx = slots.as_mut_ptr(); | ||
|
|
||
| let module = module_with_index(1); | ||
| assert!(unsafe { cycle.core_main_conf_unchecked::<u32>(&module) }.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn populated_slot_returns_typed_reference() { | ||
| let mut value: u32 = 42; | ||
| let mut slots: [CoreConfSlot; 1] = [(&raw mut value).cast()]; | ||
|
|
||
| let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| cycle.conf_ctx = slots.as_mut_ptr(); | ||
|
|
||
| let mut conf: ngx_conf_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| conf.cycle = &raw mut cycle; | ||
|
|
||
| let module = module_with_index(0); | ||
|
|
||
| let got = unsafe { conf.core_main_conf_unchecked::<u32>(&module).map(|v| *v.as_ref()) }; | ||
| assert_eq!(got, Some(42)); | ||
|
|
||
| let got_mut = | ||
| unsafe { conf.core_main_conf_unchecked::<u32>(&module).map(|mut v| v.as_mut()) }; | ||
| assert!(got_mut.is_some()); | ||
| if let Some(v) = got_mut { | ||
| *v = 99; | ||
| } | ||
| assert_eq!(value, 99); | ||
| } | ||
|
|
||
| struct TestCoreModule; | ||
|
|
||
| unsafe impl CoreModuleMainConf for TestCoreModule { | ||
| type MainConf = u32; | ||
|
|
||
| fn module() -> &'static ngx_module_t { | ||
| Box::leak(Box::new(module_with_index(0))) | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn main_conf_trait_accessors_return_typed_references() { | ||
| let mut value: u32 = 42; | ||
| let mut slots: [CoreConfSlot; 1] = [(&raw mut value).cast()]; | ||
|
|
||
| let mut cycle: ngx_cycle_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| cycle.conf_ctx = slots.as_mut_ptr(); | ||
|
|
||
| let mut conf: ngx_conf_t = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
| conf.cycle = &raw mut cycle; | ||
|
|
||
| assert_eq!(TestCoreModule::main_conf(&conf).copied(), Some(42)); | ||
|
|
||
| if let Some(v) = TestCoreModule::main_conf_mut(&conf) { | ||
| *v = 99; | ||
| } | ||
| assert_eq!(value, 99); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we may expect a special trait for core module, similar to
HttpModuletrait. To make this PR minimal, I'd suggest to include onlymodule()method to it for now. In future this will allow to add all other necessary components to this trait.