nvec 0.10.0

N-vectors and N-strings.
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright 2025-2026 Gabriel Bjørnager Jensen.
//
// SPDX: MIT OR Apache-2.0

//! The [`NVec`] type.

mod convert;
mod cmp;
mod fmt;
mod iter;
mod oct;
mod ops;
mod serde;
mod test;

use core::borrow::{Borrow, BorrowMut};
use core::hash::{Hash, Hasher};
use core::hint::assert_unchecked;
use core::mem::MaybeUninit;
use core::ptr::drop_in_place;
use core::slice;
use nvec::n_vec::TryReserveError;

#[cfg(feature = "std")]
use std::io::{self, Write};

/// An N-vector.
pub struct NVec<T, const N: usize> {
	/// The length of the vector.
	len: usize,

	/// The raw vector data.
	data: [MaybeUninit<T>; N],
}

impl<T, const N: usize> NVec<T, N> {
	/// The maximum, possible length of the N-vector.
	const MAX_LEN: usize = {
		if size_of::<T>() > 0 {
			let max_size = isize::MAX as usize;
			max_size / size_of::<T>()
		} else {
			usize::MAX
		}
	};

	/// Constructs a new, empty N-vector.
	#[inline]
	#[must_use]
	pub const fn new() -> Self {
		let data = [const { MaybeUninit::uninit() }; N];

		// SAFETY: A length of `0` is always valid.
		unsafe { Self::from_raw_parts(data, 0) }
	}

	/// Attempts to construct a new N-vector with a
	/// minimum capacity.
	///
	/// # Errors
	///
	/// As all N-vector are capacity-constrained by `N`,
	/// this constructer merely tests if the provided
	/// capacity is within the bounds of the specific
	/// `NVec` instance.
	#[inline]
	pub const fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
		if capacity <= N {
			Ok(Self::new())
		} else {
			Err(TryReserveError)
		}
	}

	/// Attempts to reserve capacity for additional
	/// elements.
	///
	/// # Errors
	///
	/// As the total capacity of the vector cannot be
	/// grown, this method will simply test if the
	/// remaining capacity is sufficient to store the
	/// specific element count: If the space is
	/// insufficient, this method will return an [`Err`]
	/// instance.
	pub const fn try_reserve(&self, extra: usize) -> Result<(), TryReserveError> {
		if extra <= self.remaining_capacity() {
			Ok(())
		} else {
			Err(TryReserveError)
		}
	}

	/// Attempts to push an element into the vector.
	///
	/// # Errors
	///
	/// This method will return an [`Err`] instance if
	/// there isn't any capacity left for the pushed
	/// object.
	pub fn try_push(&mut self, elem: T) -> Result<(), TryReserveError> {
		self.try_push_mut(elem).map(|_| ())
	}

	/// Attempts to push an element into the vector,
	/// returning a mutable reference to its slot.
	///
	/// # Errors
	///
	/// This method will return an [`Err`] instance if
	/// there isn't any capacity left for the pushed
	/// object.
	pub fn try_push_mut(&mut self, elem: T) -> Result<&mut T, TryReserveError> {
		if self.remaining_capacity() < 1 {
			return Err(TryReserveError);
		}

		let slot = unsafe { self.push_unchecked_mut(elem) };
		Ok(slot)
	}

	/// Unsafely pushes an element into the vector,
	/// returning a mutable reference to its slot.
	///
	/// # Safety
	///
	/// The vector must have enough remaining capacity
	/// for at least one more element.
	#[inline]
	pub const unsafe fn push_unchecked_mut(&mut self, elem: T) -> &mut T {
		debug_assert!(self.remaining_capacity() >= 1);

		// SAFETY: Caller guarantees that there is suffi-
		// cient capacity, in which case this offset is
		// within bounds (and not even one past the end:)
		let slot = unsafe { self.as_mut_ptr().add(self.len()) };

		// SAFETY: We have already guaranteed that the
		// pointer is within bounds, which itself implies:
		//
		// * The pointer is valid for writes.
		//
		// * The pointer is aligned.
		unsafe { slot.write(elem) };

		// SAFETY: The above slot is allocated in the spare
		// capacity, guaranteeing that this capacity is at
		// least one.
		self.len += 1;

		// SAFETY: The pointer was initially derived from a
		// mutable `MaybeUninit` reference, with
		// `MaybeUninit<T>` being guaranteed transparent to
		// `T`. The following invariants are thus kept
		// thanks to the pointer being unmodified:
		//
		// * The pointer is aligned.
		//
		// * The pointer is non-null.
		//
		// * The pointer is dereferenceable (i.e. is has
		//   both read and write provenance and points to a
		//   single allocation.)
		//
		// * The pointer is exclusive.
		//
		// We have just now made sure that:
		//
		// * The destination is a valid instance of `T`.
		unsafe { &mut *slot }
	}

	/// Attempts to resize the vector.
	///
	/// # Errors
	///
	/// This method will return an [`Err`] instance if
	/// the provided length is longer than the capacity
	/// of the vector.
	pub fn try_resize(&mut self, len: usize, value: T) -> Result<(), TryReserveError>
	where
		T: Clone,
	{
		if len > self.len() {
			let diff = self.len().abs_diff(len);

			let slots = self.spare_capacity_mut()
				.get_mut(..diff)
				.ok_or(TryReserveError)?;

			for slot in slots {
				slot.write(value.clone());
			}

			// SAFETY: We have made sure to initialise the ad-
			// ditional elements with the padding value.
			unsafe { self.set_len(len) };
		} else if len < self.len() {
			self.truncate(len);
		}

		Ok(())
	}

	/// Truncates the vector.
	///
	/// If the provided length is greater than or equal
	/// to the vector's current length, the vector
	/// remains unchanged.
	#[inline]
	pub fn truncate(&mut self, len: usize) {
		if len >= self.len() {
			return;
		}

		// SAFETY: We have above tested that the new length
		// is less than the current length.
		let to_drop: *mut _ = unsafe { self.get_unchecked_mut(len..) };

		// SAFETY: We have above tested that the new length
		// is less than the current length, so we can also
		// not expose invalid elements.
		self.len = len;

		// SAFETY: The following are guaranteed due to the
		// pointer being coerced from a mutable reference:
		//
		// * The pointer is valid for both reads and
		//   writes.
		//
		// * The pointer is aligned.
		//
		// * The pointer is non-null.
		//
		// * The destination is a valid instance of `[T]`.
		//
		// * The pointer is exclusive for the duration of
		//   the `drop_in_place` call.
		//
		// The vector length has already been reset, so we
		// can assume that these elements are not reused
		// again after the drop (unless reinitialised.)
		unsafe { drop_in_place(to_drop) };
	}

	/// Clears the vector.
	#[inline]
	pub fn clear(&mut self) {
		self.truncate(0)
	}

	/// Retrieves the total capacity of the vector.
	#[inline(always)]
	#[must_use]
	pub const fn capacity(&self) -> usize {
		N
	}

	/// Computes the amount of remaining capacity in the
	/// vector.
	#[inline]
	#[must_use]
	pub const fn remaining_capacity(&self) -> usize {
		// SAFETY: This can never underflow due to our ex-
		// istential requirement of `len() <= capacity()`.
		unsafe{ self.capacity().unchecked_sub(self.len()) }
	}

	/// Overwrites the length of the vector.
	///
	/// When decreasing the vector length, destructors
	/// are not run for the shadowed elements.
	///
	/// # Safety
	///
	/// The following prerequisites must be met when
	/// calling this method:
	///
	/// * `len` may not be greater than the capacity of
	///   the vector (i.e. `N`).
	/// * The first `len` elements of the vector must
	///   already be valid instances of `T`.
	///
	/// Behaviour is undefined if any of these
	/// conditions are violated.
	#[inline(always)]
	#[track_caller]
	pub const unsafe fn set_len(&mut self, len: usize) {
		debug_assert!(len <= self.capacity());

		self.len = len;
	}

	/// Retrieves the length of the vector.
	#[inline(always)]
	#[must_use]
	pub const fn len(&self) -> usize {
		let len = self.len;

		// SAFETY: This is always a precondition.
		unsafe { assert_unchecked(len <= N) };

		// SAFETY: This is guaranteed simply by the fact
		// that we can't allocate a buffer that is bigger
		// than this.
		unsafe { assert_unchecked(len <= Self::MAX_LEN) };

		len
	}

	/// Tests if the vector is empty.
	#[inline]
	#[must_use]
	pub const fn is_empty(&self) -> bool {
		self.len() == 0
	}

	/// Gets a slice over the inactive elements.
	#[inline]
	pub const fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
		let len = self.remaining_capacity();

		let data = {
			let base = self.as_mut_ptr().cast::<MaybeUninit<T>>();

			// SAFETY: `Self::len` always guarantees an index
			// that is at most one past the end of the alloca-
			// tion.
			unsafe { base.add(self.len()) }
		};

		// SAFETY:
		//
		// * The pointer, deriving from a reference, is
		//   guaranteed to be non-null, valid for both
		//   reads and writes, and aligned.
		//
		// * `Self::len` guarantees that all elements up to
		//   (exclusive) the returned index exist.
		//   `MaybeUninit` has no requirements for the
		//   state of these elements.
		//
		// * The lifetime of this returned reference is in-
		//   herited from `self`.
		//
		// * The total size of the slice cannot be greater
		//   than `isize::MAX` as the containing buffer
		//   object is a Rust object itself.
		unsafe { slice::from_raw_parts_mut(data, len) }
	}

	/// Copies the N-vector.
	///
	/// [`NVec`] can't implement [`Copy`] due to it also
	/// implementing [`Drop`]. This method does the op-
	/// eration one would expect from a copy.
	#[inline]
	#[must_use]
	pub const fn copied(&self) -> Self
	where
		T: Copy,
	{
		let len  = self.len;
		let data = self.data;

		// SAFETY: These parts are directly from an exist-
		// ing N-vector.
		unsafe { Self::from_raw_parts(data, len) }
	}

	/// Maps the vector elements
	#[inline]
	#[must_use]
	pub fn map<F: FnMut(T) -> U, U>(self, mut op: F) -> NVec<U, N> {
		let len = self.len();

		let mut data = [const { MaybeUninit::<U>::uninit() }; N];

		for (index, value) in self.into_iter().enumerate() {
			let slot = &mut data[index];

			let value = op(value);
			slot.write(value);
		}

		// SAFETY:
		//
		// * Each active element has been properly ini-
		//   tialised from a result of `op`.
		//
		// * The length `len` comes from an existing N-vec-
		//   tor and is therefore guaranteed to be within bounds.
		unsafe { NVec::from_raw_parts(data, len) }
	}
}

impl<T, const N: usize> Borrow<[T]> for NVec<T, N> {
	#[inline]
	fn borrow(&self) -> &[T] {
		self
	}
}

impl<T, const N: usize> BorrowMut<[T]> for NVec<T, N> {
	#[inline]
	fn borrow_mut(&mut self) -> &mut [T] {
		self
	}
}

impl<T: Clone, const N: usize> Clone for NVec<T, N> {
	#[inline]
	fn clone(&self) -> Self {
		let mut new = Self::new();

		for i in 0..self.len() {
			new.data[i].write(self[i].clone());
		}

		// SAFETY: We have just initialised all of these
		// elements.
		unsafe { new.set_len(self.len()) };

		new
	}
}
impl<T, const N: usize> Default for NVec<T, N> {
	#[inline]
	fn default() -> Self {
		Self::new()
	}
}

impl<T, const N: usize> Drop for NVec<T, N> {
	#[inline]
	fn drop(&mut self) {
		let to_drop = self.as_mut_slice();

		// SAFETY: The following are guaranteed due to our
		// passing a mutable reference:
		//
		// * The pointer is valid for both reads and
		//   writes.
		//
		// * The pointer is aligned.
		//
		// * The pointer is non-null.
		//
		// * The destination is a valid instance (slice
		//   hereof) of `T`.
		//
		// * The pointer is exclusive for the duration of
		//   the `drop_in_place` call.
		//
		// The mutable slice does not exist after this
		// call, and we guarantee that the elements are not
		// used afterwards in no other context.
		unsafe { drop_in_place(to_drop) };
	}
}

impl<T: Hash, const N: usize> Hash for NVec<T, N> {
	#[inline]
	fn hash<H: Hasher>(&self, state: &mut H) {
		self.as_slice().hash(state);
	}
}

#[cfg(feature = "std")]
impl<const N: usize> Write for NVec<u8, N> {
	#[inline]
	fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
		let len = self.remaining_capacity().min(buf.len());

		let buf = &buf[..len];
		self.write_all(buf)?;

		Ok(len)
	}

	#[inline(always)]
	fn flush(&mut self) -> io::Result<()> {
		// NOTE: We never cache anything at all.
		Ok(())
	}

	#[inline]
	fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
		let slot = self.spare_capacity_mut()
			.get_mut(..buf.len())
			.ok_or(TryReserveError)?;

		{
			let len            = buf.len();
			let dst: *mut u8   = slot.as_mut_ptr().cast::<u8>();
			let src: *const u8 = buf.as_ptr();

			// SAFETY: todo :(
			unsafe { dst.copy_from_nonoverlapping(src, len) };
		}

		Ok(())
	}
}