par_slice/impls/constructor/pointer.rs
1use crate::*;
2
3/// Utility struct for contructors for slices that allow unsynchronized access
4/// to their elements through [`PointerIndex`] and [`PointerChunkIndex`].
5pub struct PointerParSlice;
6
7impl PointerParSlice {
8 #[allow(clippy::new_ret_no_self)]
9 /// Constructs a new slice with `len` elements, each initialized
10 /// to [`T::default`](`Default::default`), that allows unsynchronized
11 /// access to its elements through [`PointerIndex`] and that can be
12 /// converted into a boxed slice.
13 ///
14 /// # Examples
15 /// ```
16 /// # use par_slice::*;
17 /// let pointer_slice = PointerParSlice::new(4);
18 ///
19 /// unsafe {
20 /// *pointer_slice.get_mut_ptr(0) = 42;
21 /// }
22 ///
23 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 0, 0, 0]);
24 /// ```
25 #[inline]
26 pub fn new<T: Default + Send + Sync>(
27 len: usize,
28 ) -> impl PointerIndex<T> + ParCollection<Box<[T]>> {
29 new_boxed_slice(len).into_pointer_par_index()
30 }
31
32 /// Constructs a new slice with `len` elements, each initialized
33 /// to `value`, that allows unsynchronized
34 /// access to its elements through [`PointerIndex`] and that can be
35 /// converted into a boxed slice.
36 ///
37 /// # Examples
38 /// ```
39 /// # use par_slice::*;
40 /// let pointer_slice = PointerParSlice::with_value(69, 4);
41 ///
42 /// unsafe {
43 /// *pointer_slice.get_mut_ptr(0) = 42;
44 /// }
45 ///
46 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 69, 69, 69]);
47 /// ```
48 #[inline]
49 pub fn with_value<T: Clone + Send + Sync>(
50 value: T,
51 len: usize,
52 ) -> impl PointerIndex<T> + ParCollection<Box<[T]>> {
53 new_boxed_slice_with_value(len, value).into_pointer_par_index()
54 }
55
56 /// Constructs a new slice with `len` elements, each initialized
57 /// to the return value of `closure` called with the index of the element
58 /// to generate as an [`usize`], that allows unsynchronized
59 /// access to its elements through [`PointerIndex`] and that can be
60 /// converted into a boxed slice.
61 ///
62 /// # Examples
63 /// ```
64 /// # use par_slice::*;
65 /// let pointer_slice = PointerParSlice::with_closure(|i| i, 4);
66 ///
67 /// unsafe {
68 /// *pointer_slice.get_mut_ptr(0) = 42;
69 /// }
70 ///
71 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 1, 2, 3]);
72 /// ```
73 #[inline]
74 pub fn with_closure<T: Send + Sync>(
75 closure: impl FnMut(usize) -> T,
76 len: usize,
77 ) -> impl PointerIndex<T> + ParCollection<Box<[T]>> {
78 new_boxed_slice_with(len, closure).into_pointer_par_index()
79 }
80
81 /// Constructs a new slice with `len` elements, each initialized
82 /// to [`T::default`](`Default::default`), that allows unsynchronized
83 /// access to chunks of `chunk_size` of its elements through
84 /// [`PointerChunkIndex`] and that can be converted into a boxed slice.
85 ///
86 /// # Examples
87 /// ```
88 /// # use par_slice::*;
89 /// let pointer_slice = PointerParSlice::new_chunks(4, 2);
90 ///
91 /// unsafe {
92 /// (*pointer_slice.get_mut_ptr(0))[0] = 42;
93 /// }
94 ///
95 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 0, 0, 0]);
96 /// ```
97 #[inline]
98 pub fn new_chunks<T: Default + Send + Sync>(
99 len: usize,
100 chunk_size: usize,
101 ) -> impl PointerChunkIndex<T> + ParCollection<Box<[T]>> {
102 assert_chunk_size(len, chunk_size);
103 new_boxed_slice(len).into_pointer_par_chunk_index(chunk_size)
104 }
105
106 /// Constructs a new slice with `len` elements, each initialized
107 /// to `value`, that allows unsynchronized
108 /// access to chunks of `chunk_size` of its elements through
109 /// [`PointerChunkIndex`] and that can be converted into a boxed slice.
110 ///
111 /// # Examples
112 /// ```
113 /// # use par_slice::*;
114 /// let pointer_slice = PointerParSlice::chunks_with_value(69, 4, 2);
115 ///
116 /// unsafe {
117 /// (*pointer_slice.get_mut_ptr(0))[0] = 42;
118 /// }
119 ///
120 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 69, 69, 69]);
121 /// ```
122 #[inline]
123 pub fn chunks_with_value<T: Clone + Send + Sync>(
124 value: T,
125 len: usize,
126 chunk_size: usize,
127 ) -> impl PointerChunkIndex<T> + ParCollection<Box<[T]>> {
128 assert_chunk_size(len, chunk_size);
129 new_boxed_slice_with_value(len, value).into_pointer_par_chunk_index(chunk_size)
130 }
131
132 /// Constructs a new slice with `len` elements, each initialized
133 /// to the return value of `closure` called with the index of the element
134 /// to generate as an [`usize`], that allows unsynchronized
135 /// access to chunks of `chunk_size` of its elements through
136 /// [`PointerChunkIndex`] and that can be converted into a boxed slice.
137 ///
138 /// # Examples
139 /// ```
140 /// # use par_slice::*;
141 /// let pointer_slice = PointerParSlice::chunks_with_closure(|i| i, 4, 2);
142 ///
143 /// unsafe {
144 /// (*pointer_slice.get_mut_ptr(0))[0] = 42;
145 /// }
146 ///
147 /// assert_eq!(pointer_slice.into().as_ref(), &[42, 1, 2, 3]);
148 /// ```
149 #[inline]
150 pub fn chunks_with_closure<T: Send + Sync>(
151 closure: impl FnMut(usize) -> T,
152 len: usize,
153 chunk_size: usize,
154 ) -> impl PointerChunkIndex<T> + ParCollection<Box<[T]>> {
155 assert_chunk_size(len, chunk_size);
156 new_boxed_slice_with(len, closure).into_pointer_par_chunk_index(chunk_size)
157 }
158}