werkzeug/src/util.rs
Janis 9e3fa2cdb0 u32,u64,u16 conversion functions, sync queue, random utilities
- `NextIf` iterator trait that yields the next element iff some predicate holds
- `u64_from_u32s`, `u32s_from_u64`, `u32_from_u16s`, `u16s_from_u32` functions
- moved `can_transmute` to `mem` module, added `is_same_size` and `is_aligned`
functions
- `copy_from` function for `TaggedAtomicPtr`
- attempt at a sync queue?
- `is_whitespace` function
2025-08-06 22:51:27 +02:00

84 lines
2.1 KiB
Rust

use core::ops::{Deref, DerefMut};
#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, Copy)]
pub struct Send<T>(pub T);
unsafe impl<T> core::marker::Send for Send<T> {}
impl<T> Deref for Send<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Send<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Send<T> {
pub unsafe fn new(value: T) -> Self {
Self(value)
}
pub fn into_inner(self) -> T {
self.0
}
}
/// returns the number of available hardware threads, or 1 if it cannot be determined.
#[cfg(feature = "std")]
pub fn available_parallelism() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}
#[cfg(feature = "std")]
pub fn unwrap_or_panic<T>(result: std::thread::Result<T>) -> T {
match result {
Ok(value) => value,
Err(payload) => std::panic::resume_unwind(payload),
}
}
#[deprecated(
since = "0.1.0",
note = "use `can_transmute` from `mem` module instead"
)]
pub use super::mem::can_transmute;
/// True if `c` is considered a whitespace according to Rust language definition.
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
/// for definitions of these classes.
pub fn is_whitespace(c: char) -> bool {
// This is Pattern_White_Space.
//
// Note that this set is stable (ie, it doesn't change with different
// Unicode versions), so it's ok to just hard-code the values.
matches!(
c,
// Usual ASCII suspects
'\u{0009}' // \t
| '\u{000A}' // \n
| '\u{000B}' // vertical tab
| '\u{000C}' // form feed
| '\u{000D}' // \r
| '\u{0020}' // space
// NEXT LINE from latin1
| '\u{0085}'
// Bidi markers
| '\u{200E}' // LEFT-TO-RIGHT MARK
| '\u{200F}' // RIGHT-TO-LEFT MARK
// Dedicated whitespace characters from Unicode
| '\u{2028}' // LINE SEPARATOR
| '\u{2029}' // PARAGRAPH SEPARATOR
)
}