Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZeroVec::as_slice #5621

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions utils/zerovec/src/zerovec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ impl<'a, T: AsULE> Deref for ZeroVec<'a, T> {
type Target = ZeroSlice<T>;
#[inline]
fn deref(&self) -> &Self::Target {
let slice: &[T::ULE] = self.vector.as_slice();
ZeroSlice::from_ule_slice(slice)
self.as_slice()
}
}

Expand Down Expand Up @@ -193,7 +192,7 @@ impl<'a, T: AsULE> Clone for ZeroVec<'a, T> {

impl<'a, T: AsULE> AsRef<ZeroSlice<T>> for ZeroVec<'a, T> {
fn as_ref(&self) -> &ZeroSlice<T> {
self.deref()
self.as_slice()
}
}

Expand Down Expand Up @@ -429,6 +428,16 @@ impl<'a, T: AsULE> ZeroVec<'a, T> {
}
}

/// Returns this [`ZeroVec`] as a [`ZeroSlice`].
///
/// To get a reference with a longer lifetime from a borrowed [`ZeroVec`],
/// use [`ZeroVec::as_maybe_borrowed`].
#[inline]
pub const fn as_slice(&self) -> &ZeroSlice<T> {
let slice: &[T::ULE] = self.vector.as_slice();
ZeroSlice::from_ule_slice(slice)
}

/// Casts a `ZeroVec<T>` to a compatible `ZeroVec<P>`.
///
/// `T` and `P` are compatible if they have the same `ULE` representation.
Expand Down Expand Up @@ -567,8 +576,11 @@ impl<'a, T: AsULE> ZeroVec<'a, T> {
self.vector.capacity != 0
}

/// If this is a borrowed ZeroVec, return it as a slice that covers
/// its lifetime parameter
/// If this is a borrowed [`ZeroVec`], return it as a slice that covers
/// its lifetime parameter.
///
/// To infallibly get a [`ZeroSlice`] with a shorter lifetime, use
/// [`ZeroVec::as_slice`].
#[inline]
pub fn as_maybe_borrowed(&self) -> Option<&'a ZeroSlice<T>> {
if self.is_owned() {
Expand Down