1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
//! Atomic abstractions and thread-safe appendable list with lock-free iterators
//!
//! # Features
//! - [`Atomic abstractions (Atomic, AtomicOption, FillOnceAtomicOption, FillOnceAtomicArc)`]
//! - [`Thread-safe appendable list with a lock-free iterator (VoluntaryServitude - also called VS)`]
//! - [`Serde serialization ("serde-traits" feature)`]
//! - [`Call this code from C (FFI)`] (also in **./examples**)
//! - [`System Allocator ("system-alloc" feature)`]
//! - [`Logging ("logs" feature)`]
//!
//! # Atomic abstractions
//! - [`Atomic`] -> atomic `Box<T>`
//! - [`AtomicOption`] -> atomic `Option<Box<T>>`
//! - [`FillOnceAtomicOption`] -> atomic `Option<Box<T>>` that can give references (ideal for iterators)
//! - [`FillOnceAtomicArc`] -> atomic `Option<Arc<T>>` with a limited Api (like [`FillOnceAtomicOption`])
//!
//! With [`Atomic`] and [`AtomicOption`] it's not safe to get a reference, you must replace the
//! value to access it
//!
//! To safely get a reference to T you must use [`FillOnceAtomicOption`] and accept the API limitations
//!
//! A safe `AtomicArc` is impossible, so you must use `ArcCell` from crossbeam (locks to clone) or [`FillOnceAtomicArc`]
//!
//! # Thread-safe appendable list that can create a lock-free iterator
//! - [`VoluntaryServitude`] (also called [`VS`])
//!
//! # Api of `VS` Iterator
//! - [`Iter`]
//!
//! [`Atomic`]: ./struct.Atomic.html
//! [`AtomicOption`]: ./struct.AtomicOption.html
//! [`FillOnceAtomicOption`]: ./struct.FillOnceAtomicOption.html
//! [`FillOnceAtomicArc`]: ./struct.FillOnceAtomicArc.html
//! [`Atomic abstractions (Atomic, AtomicOption, FillOnceAtomicOption, FillOnceAtomicArc)`]: #atomic-abstractions
//! [`Thread-safe appendable list with a lock-free iterator (VoluntaryServitude - also called VS)`]: ./struct.VoluntaryServitude.html
//! [`Serde serialization ("serde-traits" feature)`]: ./serde/index.html
//! [`Call this code from C (FFI)`]: ./ffi/index.html
//! [`System Allocator ("system-alloc" feature)`]: ./static.GLOBAL_ALLOC.html
//! [`VoluntaryServitude`]: ./struct.VoluntaryServitude.html
//! [`VS`]: ./type.VS.html
//! [`Iter`]: ./struct.Iter.html
//! [`Logging ("logs" feature)`]: ./fn.setup_logger.html
#![cfg_attr(docs_rs_workaround, feature(allocator_api))]
#![cfg_attr(docs_rs_workaround, feature(global_allocator))]
#![cfg_attr(docs_rs_workaround, feature(doc_cfg))]
#![deny(
missing_debug_implementations,
missing_docs,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results,
bad_style,
const_err,
dead_code,
improper_ctypes,
legacy_directory_ownership,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
plugin_as_library,
private_in_public,
private_no_mangle_fns,
private_no_mangle_statics,
safe_extern_statics,
unconditional_recursion,
unions_with_drop_fields,
unused_allocation,
unused_comparisons,
unused_parens,
while_true
)]
#![doc(test(attr(deny(warnings))))]
#![doc(html_root_url = "https://docs.rs/voluntary_servitude/3.0.1/voluntary-servitude")]
#[cfg(feature = "serde-traits")]
extern crate serde as serde_lib;
#[cfg(feature = "rayon-traits")]
extern crate rayon as rayon_lib;
#[cfg(feature = "system-alloc")]
use std::alloc::System;
/// Represents the use of the system's allocator instead of rust's default
///
/// By default is disabled, but can be enabled with the `system-alloc` feature
/// It's intended to be used by the FFI, but you can use it in rust by setting in Cargo.toml
///
/// ```bash
/// cargo build --release --features "system-alloc"
/// ```
///
/// *`./dist/libvoluntary_servitude.so` (FFI) is compiled with the system's allocator*
#[cfg(feature = "system-alloc")]
#[cfg_attr(docs_rs_workaround, doc(cfg(feature = "system-alloc")))]
#[global_allocator]
pub static GLOBAL_ALLOC: System = System;
#[cfg(not(feature = "system-alloc"))]
/// System allocator is not enabled, it's available behind the `system-alloc` feature flag
///
/// It's intended to be used by the FFI, but you can use it in rust by setting in Cargo.toml
///
/// ```bash
/// cargo build --release --features "system-alloc"
/// ```
///
/// *`./dist/libvoluntary_servitude.so` (FFI) is compiled with system allocator*
pub static GLOBAL_ALLOC: () = ();
extern crate crossbeam;
#[macro_use]
#[cfg(feature = "logs")]
extern crate log;
#[cfg(feature = "logs")]
extern crate env_logger;
/// Setup logger according to `RUST_LOG` env var (must enable `logs` feature)
///
/// *During tests log to stdout to supress output on passes*
///
/// # Enable the feature:
///
/// **Cargo.toml**
/// ```toml
/// [dependencies]
/// voluntary_servitude = { version = "3", features = "logs" }
/// ```
///
/// # Set the `RUST_LOG` env var:
/// ```bash
/// export RUST_LOG=voluntary_servitude=trace
/// export RUST_LOG=voluntary_servitude=debug
/// export RUST_LOG=voluntary_servitude=info
/// export RUST_LOG=voluntary_servitude=warn
/// export RUST_LOG=voluntary_servitude=error
/// ```
///
/// ```rust
/// // Must enable the `logs` feature and set the appropriate `RUST_LOG` env var
/// voluntary_servitude::setup_logger();
/// // Call code to be logged
/// // ...
/// ```
#[cfg(feature = "logs")]
#[cfg_attr(docs_rs_workaround, doc(cfg(feature = "logs")))]
#[inline]
pub fn setup_logger() {
/// Ensures logger is only initialized once
static STARTED: std::sync::Once = std::sync::ONCE_INIT;
STARTED.call_once(env_logger::init);
}
/// Enum impossible to construct (hint that the code is unreachable)
#[cfg(not(feature = "logs"))]
#[doc(hidden)]
#[derive(Debug)]
pub enum ImpossibleToInstantiate {}
/// Logging is not enabled, it's available behind the `logs` feature flag
///
/// When "logs" is set the function `setup_logger` will be available to start logging the execution
///
/// # Enable the feature:
/// **Cargo.toml**
/// ```toml
/// [dependencies]
/// voluntary_servitude = { version = "3", features = "logs" }
/// ```
///
/// # See full docs:
/// ```bash
/// cargo doc --all-features --open
/// ```
///
/// # Set the `RUST_LOG` env var:
/// ```bash
/// export RUST_LOG=voluntary_servitude=trace
/// export RUST_LOG=voluntary_servitude=debug
/// export RUST_LOG=voluntary_servitude=info
/// export RUST_LOG=voluntary_servitude=warn
/// export RUST_LOG=voluntary_servitude=error
/// ```
///
/// ```_rust
/// // Must enable the `logs` feature and set the appropriate `RUST_LOG` env var
/// voluntary_servitude::setup_logger();
/// // Call code to be logged
/// // ...
/// ```
#[cfg(not(feature = "logs"))]
#[inline]
pub fn setup_logger(_: ImpossibleToInstantiate) {}
/// Remove logging macros when they are disabled (at compile time)
#[macro_use]
#[cfg(not(feature = "logs"))]
#[allow(unused)]
mod mock {
macro_rules! trace(($($x:tt)*) => ());
macro_rules! debug(($($x:tt)*) => ());
macro_rules! info(($($x:tt)*) => ());
macro_rules! warn(($($x:tt)*) => ());
macro_rules! error(($($x:tt)*) => ());
}
#[macro_use]
mod macros;
mod atomic;
mod atomic_option;
pub mod ffi;
mod fill_once_atomic_arc;
mod fill_once_atomic_option;
mod iterator;
mod node;
mod voluntary_servitude;
#[cfg(feature = "serde-traits")]
#[cfg_attr(docs_rs_workaround, doc(cfg(feature = "serde-traits")))]
pub mod serde;
#[cfg(not(feature = "serde-traits"))]
pub mod serde {
//! Serde integration is not enabled, it's available behind `serde-traits` feature flag
//!
//! This feature provides access to serde's `Serialize`/`Deserialize` trait implementation for [`VoluntaryServitude`]
//!
//! [`VoluntaryServitude`]: ../struct.VoluntaryServitude.html#implementations
//!
//! # Enable the feature:
//!
//! **Cargo.toml**
//!
//! ```toml
//! [dependencies]
//! voluntary_servitude = { version = "3", features = "logs" }
//! ```
//!
//! # See full docs:
//!
//! ```bash
//! cargo doc --all-features --open
//! ```
//!
//! # To test integration with serde `serde-tests` must also be enabled
//!
//! ```bash
//! cargo test --features "serde-traits serde-tests"
//! ```
}
pub use atomic::Atomic;
pub use atomic_option::{AtomicOption, NotEmpty};
pub use fill_once_atomic_arc::FillOnceAtomicArc;
pub use fill_once_atomic_option::FillOnceAtomicOption;
pub use iterator::Iter;
pub use voluntary_servitude::{VoluntaryServitude, VS};
use std::ptr::{null_mut, NonNull};
/// Trait made to simplify conversion between smart pointers and raw pointers
pub(crate) trait IntoPtr<T> {
/// Converts itself into a mutable pointer to it (leak or unwrap things)
fn into_ptr(self) -> *mut T;
}
impl<T> IntoPtr<T> for T {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut Self {
Box::into_raw(Box::new(self))
}
}
impl<T> IntoPtr<T> for *mut T {
#[inline]
#[must_use]
fn into_ptr(self) -> Self {
self
}
}
impl<T> IntoPtr<T> for Option<*mut T> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut T {
self.unwrap_or(null_mut())
}
}
impl<T> IntoPtr<T> for Option<NonNull<T>> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut T {
self.map_or(null_mut(), |nn| nn.as_ptr())
}
}
impl<T> IntoPtr<T> for Box<T> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut T {
Self::into_raw(self)
}
}
impl<T> IntoPtr<T> for Option<T> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut T {
self.map_or(null_mut(), |v| v.into_ptr())
}
}
impl<T> IntoPtr<T> for Option<Box<T>> {
#[inline]
#[must_use]
fn into_ptr(self) -> *mut T {
self.map_or(null_mut(), Box::into_raw)
}
}
/// Trait to make easier removing impossible branchs from first-order types
pub(crate) trait Filled<T> {
/// Marks that type is always filled (panics in debug, branch is removed in release)
///
/// It will return the inner value
unsafe fn filled(self, msg: &str) -> T;
/// Marks that type is always filled and returns `Default` of what is expected (panics in debug, branch is removed in release)
/// avoids a extra `map(|_| U::default())`
unsafe fn filled_default<U: Default>(self, msg: &str) -> U;
}
impl<T, E> Filled<T> for Result<T, E> {
#[inline]
#[allow(unused)]
unsafe fn filled(self, msg: &str) -> T {
self.unwrap_or_else(|_| {
#[cfg(debug_assertions)]
panic!("{}", msg);
#[cfg(not(debug_assertions))]
::std::hint::unreachable_unchecked();
})
}
#[inline]
#[allow(unused)]
unsafe fn filled_default<U: Default>(self, msg: &str) -> U {
let _ = self.filled(msg);
U::default()
}
}