diff --git a/Cargo.lock b/Cargo.lock index 8c10247..95382ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,9 @@ name = "allocator-api2" version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +dependencies = [ + "serde", +] [[package]] name = "android-tzdata" @@ -821,6 +824,26 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.10.9" diff --git a/Cargo.toml b/Cargo.toml index 7799118..a035054 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,10 @@ async = [ # Enables the components using memory allocation. # If no `std` flag, `alloc` crate is internally used instead. This flag is mainly for `no_std` build. alloc = ["allocator-api2/alloc"] +# Enables serde support for some of the provided types. +serde = [ + "allocator-api2/serde", +] # Enables the components using `std` crate. std = [ "alloc", diff --git a/examples/shared_dict.rs b/examples/shared_dict.rs index 5f293e9..0c02e0f 100644 --- a/examples/shared_dict.rs +++ b/examples/shared_dict.rs @@ -10,9 +10,8 @@ use nginx_sys::{ NGX_HTTP_MAIN_CONF, NGX_HTTP_MAIN_CONF_OFFSET, NGX_HTTP_MODULE, NGX_HTTP_VAR_CHANGEABLE, NGX_HTTP_VAR_NOCACHEABLE, NGX_LOG_EMERG, }; -use ngx::core::{ - NgxStr, NgxString, Pool, RbTreeMap, SlabPool, Status, NGX_CONF_ERROR, NGX_CONF_OK, -}; +use ngx::collections::RbTreeMap; +use ngx::core::{NgxStr, NgxString, Pool, SlabPool, Status, NGX_CONF_ERROR, NGX_CONF_OK}; use ngx::http::{HttpModule, HttpModuleMainConf}; use ngx::{ngx_conf_log_error, ngx_log_debug, ngx_string}; diff --git a/src/allocator.rs b/src/allocator.rs index 15fa4d6..685e428 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -11,10 +11,10 @@ use ::core::alloc::Layout; use ::core::mem; use ::core::ptr::{self, NonNull}; -pub use allocator_api2::alloc::{AllocError, Allocator, Global}; +pub use allocator_api2::alloc::{AllocError, Allocator}; #[cfg(feature = "alloc")] -pub use allocator_api2::{boxed, collections, vec}; +pub use allocator_api2::{alloc::Global, boxed::Box}; /// Explicitly duplicate an object using the specified Allocator. pub trait TryCloneIn: Sized { @@ -81,3 +81,24 @@ mod impls { } } } + +/// Allows turning a [`Box`][Box] into a [`Box`][Box] where `T` can be +/// unsizing-coerced into a `U`. +/// +/// See [allocator_api2::unsize_box] for an explanation why this macro is necessary. +#[cfg(feature = "alloc")] +#[doc(inline)] +pub use crate::__unsize_box as unsize_box; + +// We have to reimplement this macro because the original implementation is not reexportable. +// Macro definitions float to the top of the crate, thus we also mark it as hidden and reexport +// again from the right namespace. +#[cfg(feature = "alloc")] +#[doc(hidden)] +#[macro_export] +macro_rules! __unsize_box { + ( $boxed:expr $(,)? ) => {{ + let (ptr, alloc) = $crate::allocator::Box::into_raw_with_allocator($boxed); + unsafe { $crate::allocator::Box::from_raw_in(ptr as *mut _, alloc) } + }}; +} diff --git a/src/collections/mod.rs b/src/collections/mod.rs new file mode 100644 index 0000000..5689cd0 --- /dev/null +++ b/src/collections/mod.rs @@ -0,0 +1,15 @@ +//! Collection types. +//! +//! This module provides common collection types, mostly implemented as wrappers over the +//! corresponding NGINX types. + +#[cfg(feature = "alloc")] +pub use allocator_api2::{ + collections::{TryReserveError, TryReserveErrorKind}, + vec, // reexport both the module and the macro + vec::Vec, +}; + +pub use rbtree::RbTreeMap; + +pub mod rbtree; diff --git a/src/core/rbtree.rs b/src/collections/rbtree.rs similarity index 100% rename from src/core/rbtree.rs rename to src/collections/rbtree.rs diff --git a/src/core/mod.rs b/src/core/mod.rs index 31768df..ad8264c 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,13 +1,11 @@ mod buffer; mod pool; -pub mod rbtree; pub mod slab; mod status; mod string; pub use buffer::*; pub use pool::*; -pub use rbtree::RbTreeMap; pub use slab::SlabPool; pub use status::*; pub use string::*; diff --git a/src/core/string.rs b/src/core/string.rs index 48bf042..c43b994 100644 --- a/src/core/string.rs +++ b/src/core/string.rs @@ -207,9 +207,8 @@ mod _alloc { use super::*; - use crate::allocator::collections::TryReserveError; - use crate::allocator::vec::Vec; use crate::allocator::{self, Allocator}; + use crate::collections::{TryReserveError, Vec}; /// Owned byte string type with Allocator support. /// diff --git a/src/lib.rs b/src/lib.rs index 0dd4fc3..6bc96a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ extern crate std; pub mod allocator; #[cfg(feature = "async")] pub mod async_; +pub mod collections; /// The core module. ///