Clean up code; Fix '<{IntoIter, Vec} as Drop>::drop'; Update 'track_caller' usage;

This commit is contained in:
Gabriel Bjørnager Jensen 2025-01-30 15:53:25 +01:00
parent 4a65eb1b42
commit 914886fbb0
7 changed files with 39 additions and 21 deletions

View file

@ -3,6 +3,12 @@
This is the changelog of [Oct](https://crates.io/crates/oct/).
See `README.md` for more information.
## 0.17.1
* Clean up code
* Fix `<{IntoIter, Vec} as Drop>::drop`
* Update `track_caller` usage
## 0.17.0
* Update signature for `Slot::write`

View file

@ -3,7 +3,7 @@ members = ["oct", "oct-benchmarks", "oct-macros"]
resolver = "2"
[workspace.package]
version = "0.17.0"
version = "0.17.1"
authors = ["Gabriel Bjørnager Jensen"]
readme = "README.md"
repository = "https://gitlab.com/bjoernager/oct/"

View file

@ -32,7 +32,7 @@ readme.workspace = true
repository.workspace = true
[dependencies]
oct = { path = "../oct", version = "0.17.0", features = ["proc-macro"]}
oct = { path = "../oct", version = "0.17", features = ["proc-macro"]}
bincode = "1.3.0"
rand = "0.8.0"

View file

@ -36,7 +36,7 @@ f128 = []
f16 = []
[dependencies]
oct-macros = { path = "../oct-macros", version = "0.17.0", optional = true}
oct-macros = { path = "../oct-macros", version = "0.17", optional = true}
[lints]
workspace = true

View file

@ -229,7 +229,8 @@ impl<const N: usize> String<N> {
#[inline(always)]
#[must_use]
pub fn is_char_boundary(&self, index: usize) -> bool {
// TODO: Mark with `const`.
// TODO: Mark with `const` when `const_is_char_
// boundary` lands.
self.as_str().is_char_boundary(index)
}
@ -524,6 +525,7 @@ impl<I: SliceIndex<str>, const N: usize> Index<I> for String<N> {
type Output = I::Output;
#[inline(always)]
#[track_caller]
fn index(&self, index: I) -> &Self::Output {
self.get(index).unwrap()
}
@ -531,6 +533,7 @@ impl<I: SliceIndex<str>, const N: usize> Index<I> for String<N> {
impl<I: SliceIndex<str>, const N: usize> IndexMut<I> for String<N> {
#[inline(always)]
#[track_caller]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
self.get_mut(index).unwrap()
}
@ -660,8 +663,8 @@ impl<const N: usize> PartialEq<String<N>> for alloc::string::String {
// NOTE: This function is used by the `str` macro
// to circumvent itself using code which may be
// forbidden by the macro user's lints. This func-
// tion is sound, but please do not call it direct-
// forbidden by the macro user's lints. While this
// function is sound, please do not call it direct-
// ly. It is not a breaking change if it is re-
// moved.
#[doc(hidden)]

View file

@ -14,6 +14,8 @@ use core::mem::MaybeUninit;
use core::ptr::drop_in_place;
use core::slice;
// TODO: Implement `Copy` for `IntoIter`.
/// Owning iterator to a vector.
///
/// This type is exclusively used by the deconstruction of the [`Vec`](crate::vec::Vec) type.
@ -81,6 +83,7 @@ impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
#[inline]
fn clone(&self) -> Self {
let mut buf = [const { MaybeUninit::<T>::uninit() }; N];
let Self { pos, len, .. } = *self;
let start = pos;
@ -120,12 +123,11 @@ impl<T, const N: usize> Drop for IntoIter<T, N> {
fn drop(&mut self) {
// Drop every element that hasn't been consumed.
let remaining = self.as_mut_slice();
let remaining = &raw mut *self.as_mut_slice();
unsafe { drop_in_place(remaining) };
// We do not need to ensure that `self` is in a
// valid state after this call to `drop`.
// `MaybeUninit` also doesn't run destructors.
}
}
@ -154,11 +156,11 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
fn nth(&mut self, index: usize) -> Option<Self::Item> {
if index > self.len { return None };
let skipped = {
let skipped = unsafe {
let start = self.pos;
let stop = start + index - 0x1;
unsafe { self.buf.get_unchecked_mut(start..stop) }
self.buf.get_unchecked_mut(start..stop)
};
// Drop each skipped element.

View file

@ -430,7 +430,8 @@ impl<T: Decode, const N: usize> Decode for Vec<T, N> {
#[inline]
fn decode(input: &mut decode::Input) -> Result<Self, Self::Error> {
let len = Decode::decode(input).unwrap();
let Ok(len) = Decode::decode(input);
if len > N {
return Err(CollectionDecodeError::BadLength(LengthError {
remaining: N,
@ -460,8 +461,10 @@ impl<T, const N: usize> Default for Vec<T, N> {
unsafe {
let buf = [const { MaybeUninit::uninit() }; N];
// SAFETY: The resulting slice is zero lengthed.
Self::from_raw_parts(buf, 0x0)
// SAFETY: The resulting vector is zero lengthed
// and does therefore not expose any uninitialised
// objects.
Self::from_raw_parts(buf, Default::default())
}
}
}
@ -487,12 +490,11 @@ impl<T, const N: usize> Drop for Vec<T, N> {
fn drop(&mut self) {
// Drop every element that is currently alive.
let remaining = self.as_mut_slice();
let remaining = &raw mut *self.as_mut_slice();
unsafe { drop_in_place(remaining) };
// We do not need to ensure that `self` is in a
// valid state after this call to `drop`.
// `MaybeUninit` also doesn't run destructors.
}
}
@ -511,9 +513,12 @@ impl<T, const N: usize> From<[T; N]> for Vec<T, N> {
#[inline(always)]
fn from(value: [T; N]) -> Self {
unsafe {
let buf = value.as_ptr().cast::<[MaybeUninit<T>; N]>().read();
let buf = value
.as_ptr()
.cast::<[MaybeUninit<T>; N]>()
.read();
Self { buf, len: N }
Self { len: N, buf }
}
}
}
@ -523,8 +528,8 @@ impl<T, const N: usize> FromIterator<T> for Vec<T, N> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut iter = iter.into_iter();
let mut buf = [const { MaybeUninit::<T>::uninit() }; N];
let mut len = 0x0;
let mut buf = [const { MaybeUninit::<T>::uninit() }; N];
for item in &mut buf {
let Some(value) = iter.next() else { break };
@ -550,6 +555,7 @@ impl<T, I: SliceIndex<[T]>, const N: usize> Index<I> for Vec<T, N> {
type Output = I::Output;
#[inline(always)]
#[track_caller]
fn index(&self, index: I) -> &Self::Output {
self.get(index).unwrap()
}
@ -557,6 +563,7 @@ impl<T, I: SliceIndex<[T]>, const N: usize> Index<I> for Vec<T, N> {
impl<T, I: SliceIndex<[T]>, const N: usize> IndexMut<I> for Vec<T, N> {
#[inline(always)]
#[track_caller]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
self.get_mut(index).unwrap()
}
@ -690,8 +697,8 @@ impl<T: PartialEq<U>, U, const N: usize> PartialEq<Vec<U, N>> for alloc::vec::Ve
// NOTE: This function is used by the `vec` macro
// to circumvent itself using code which may be
// forbidden by the macro user's lints. This func-
// tion is sound, but please do not call it direct-
// forbidden by the macro user's lints. While this
// function is sound, please do not call it direct-
// ly. It is not a breaking change if it is re-
// moved.
#[doc(hidden)]
@ -703,8 +710,8 @@ pub const fn __vec<T, const N: usize, const M: usize>(data: [T; N]) -> Vec<T, M>
let data = ManuallyDrop::new(data);
let mut buf = [const { MaybeUninit::uninit() }; M];
let len = N;
let mut buf = [const { MaybeUninit::uninit() }; M];
unsafe {
let src = (&raw const data).cast();