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
use PhantomData;
use crateMelinoeCell;
use WriterShard;
/// Indexed, random-access view over the disjoint `chunk_size`-cell partitions of
/// a branded region — the *indexed* counterpart to the sequential
/// [`ShardChunks`](super::ShardChunks) lending iterator.
///
/// [`ShardChunks`](super::ShardChunks) yields shards front-to-back, each `next()` reborrowing the
/// remainder, so a consumer must thread `&mut` state through the sequence. A
/// work-stealing pool instead wants "give me partition `c` on demand" from many
/// worker threads at once. `ParChunks` provides exactly that: it holds the base
/// pointer, length, and chunk size of the region, and
/// [`get_unchecked_chunk`](Self::get_unchecked_chunk) computes the disjoint
/// sub-shard for an index without any sequential threading.
///
/// This is the single authoritative home for the `from_raw_parts_mut` range
/// math that a parallel driver needs. It is consumed by Melinoe's own
/// `std`-gated partition driver and is intended for external work-stealing
/// executors such as `moirai-parallel`, which would otherwise re-derive the same
/// unsafe sub-slicing.
///
/// The number of partitions is [`len`](Self::len) `= ceil(region_len /
/// chunk_size)` (`0` for an empty region). Each returned [`WriterShard`] carries
/// the region's `'a` borrow and `'brand`, so it obeys the same read/write
/// capability discipline as a shard from [`split_at`](WriterShard::split_at) or
/// [`chunks`](WriterShard::chunks).
///
/// # Examples
///
/// Fetch partitions by index and write each disjointly:
///
/// ```
/// use melinoe::{brand_scope, MelinoeCell};
/// use melinoe::region::WriterShard;
///
/// brand_scope(|token| {
/// let mut cells: [MelinoeCell<'_, usize>; 6] =
/// core::array::from_fn(|_| MelinoeCell::new(0));
///
/// let par = WriterShard::new(&mut cells).par_chunks(2);
/// assert_eq!(par.len(), 3); // ceil(6 / 2)
///
/// // Two *distinct* indices yield non-aliasing shards, so both writes land.
/// // SAFETY: indices 0 and 2 are distinct and each requested exactly once.
/// let mut c0 = unsafe { par.get_unchecked_chunk(0) };
/// let mut c2 = unsafe { par.get_unchecked_chunk(2) };
/// for slot in c0.iter_mut() { *slot = 10; }
/// for slot in c2.iter_mut() { *slot = 20; }
/// drop((c0, c2));
///
/// let snap = token.share();
/// let seen: [usize; 6] = core::array::from_fn(|k| *cells[k].borrow(snap));
/// assert_eq!(seen, [10, 10, 0, 0, 20, 20]);
/// });
/// ```
///
/// A shard obtained from a partition view cannot escape the brand scope — the
/// invariant `'brand` and the `'a` region borrow both pin it:
///
/// ```compile_fail
/// use melinoe::{brand_scope, MelinoeCell};
/// use melinoe::region::WriterShard;
///
/// let escaped = brand_scope(|_token| {
/// let mut cells: [MelinoeCell<'_, usize>; 4] =
/// core::array::from_fn(|_| MelinoeCell::new(0));
/// let par = WriterShard::new(&mut cells).par_chunks(2);
/// // SAFETY: index 0 requested exactly once — sound *inside* the scope.
/// unsafe { par.get_unchecked_chunk(0) } // ERROR: shard borrows `cells`/`'brand`
/// });
/// let _ = escaped;
/// ```
// SAFETY: `ParChunks` is a partition view over `&'a mut [MelinoeCell<'brand,
// T>]`; the raw `base` pointer stands in for that exclusive borrow, whose
// `Send`/`Sync` posture is governed by `MelinoeCell<'brand, T>`. It is `Send`
// exactly when the borrowed slice is (`T: Send`), matching `WriterShard`, so a
// driver can move the view (and the shards it vends) across threads.
unsafe
// SAFETY: sharing `&ParChunks` only permits calling `len`/`range`/
// `get_unchecked_chunk`; the last vends `&mut` sub-slices, so concurrent shared
// access is sound exactly when concurrent access to the underlying
// `&mut [MelinoeCell<'brand, T>]` is, i.e. `T: Send` — identical to
// `WriterShard`'s `Sync` posture.
unsafe
// Cross-check that `ParChunks::len` agrees with the sequential `ShardChunks`
// count for the same region and chunk size, so the two partition primitives can
// never disagree on how many shards tile a region. Uses a fixed-capacity backing
// array to stay `no_std`/alloc-free.