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
#[cfg(feature = "alloc")]
cfg_group! {
    use alloc::vec::Vec;
    use alloc::boxed::Box;
    use core::mem::MaybeUninit;
}

#[cfg(all(feature = "alloc", feature = "utf8"))]
use alloc::string::String;

pub fn default<T: Default>() -> T {
    T::default()
}

pub fn default_with<T: Default>(f: impl FnOnce(&mut T)) -> T {
    let mut t = T::default();
    f(&mut t);
    t
}

pub fn map_collect<C, T, I, F>(iterable: I, f: F) -> C
where
    I: IntoIterator,
    F: FnMut(I::Item) -> T,
    C: FromIterator<T>,
{
    iterable.into_iter().map(f).collect()
}

#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
pub fn map_collect_vec<T, I, F>(iterable: I, f: F) -> Vec<T>
where
    I: IntoIterator,
    F: FnMut(I::Item) -> T,
{
    iterable.into_iter().map(f).collect()
}

#[cfg_attr(docsrs, doc(cfg(feature = "ascii")))]
#[cfg(feature = "ascii")]
pub fn str_from_ascii(bytes: &[u8]) -> Option<&str> {
    // TODO(blocking): use `unicode_simd::from_ascii`
    bytes.is_ascii().then(|| unsafe { core::str::from_utf8_unchecked(bytes) })
}

#[cfg_attr(docsrs, doc(cfg(feature = "utf8")))]
#[cfg(feature = "utf8")]
pub fn str_from_utf8(bytes: &[u8]) -> Option<&str> {
    simdutf8::basic::from_utf8(bytes).ok()
}

#[cfg_attr(docsrs, doc(cfg(all(feature = "utf8", feature = "alloc"))))]
#[cfg(all(feature = "utf8", feature = "alloc"))]
pub fn string_from_utf8(bytes: Vec<u8>) -> Option<String> {
    let is_utf8 = simdutf8::basic::from_utf8(&bytes).is_ok();
    is_utf8.then(|| unsafe { String::from_utf8_unchecked(bytes) })
}

pub fn cast_ptr<T: ?Sized, U>(p: &T) -> *const U {
    <*const T>::cast(p)
}

pub fn cast_ptr_mut<T: ?Sized, U>(p: &mut T) -> *mut U {
    <*mut T>::cast(p)
}

/// [`Box::new_uninit`](Box::new_uninit)
///
/// See <https://github.com/rust-lang/rust/issues/63291>
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
pub fn box_new_uninit<T>() -> Box<MaybeUninit<T>> {
    use alloc::alloc::{alloc, handle_alloc_error};
    use core::alloc::Layout;

    // TODO: inline_const
    assert!(core::mem::size_of::<T>() != 0);

    let layout = Layout::new::<T>();
    unsafe {
        let ptr = alloc(layout);
        if ptr.is_null() {
            handle_alloc_error(layout)
        }
        Box::from_raw(ptr.cast())
    }
}

/// [`Box::new_zeroed`](Box::new_zeroed)
///
/// See <https://github.com/rust-lang/rust/issues/63291>
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
pub fn box_new_zeroed<T>() -> Box<MaybeUninit<T>> {
    use alloc::alloc::{alloc_zeroed, handle_alloc_error};
    use core::alloc::Layout;

    // TODO: inline_const
    assert!(core::mem::size_of::<T>() != 0);

    let layout = Layout::new::<T>();
    unsafe {
        let ptr = alloc_zeroed(layout);
        if ptr.is_null() {
            handle_alloc_error(layout)
        }
        Box::from_raw(ptr.cast())
    }
}

/// [`Box::assume_init`](Box::assume_init)
///
/// See <https://github.com/rust-lang/rust/issues/63291>
///
/// # Safety
/// See [`Box::assume_init`](Box::assume_init)
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
pub unsafe fn box_assume_init<T>(b: Box<MaybeUninit<T>>) -> Box<T> {
    let ptr = Box::into_raw(b).cast::<T>();
    Box::from_raw(ptr)
}