rusticl/util: add Properties::is_empty() and len()

Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32268>
This commit is contained in:
Karol Herbst 2024-11-27 15:23:57 +01:00 committed by Marge Bot
parent ef9910df4f
commit da5cf9414e
2 changed files with 21 additions and 5 deletions

View file

@ -404,7 +404,7 @@ where
fn count(&self) -> usize {
// Properties are value pairs terminated with a 0 value.
self.props.len() * 2 + 1
self.len() * 2 + 1
}
fn write_to(&self, out: &mut [MaybeUninit<T>]) {
@ -414,7 +414,7 @@ where
}
// need to terminate with a 0 value
out[self.props.len() * 2].write(T::default());
out[self.len() * 2].write(T::default());
}
}

View file

@ -2,9 +2,12 @@ pub struct Properties<T> {
pub props: Vec<(T, T)>,
}
impl<T: Copy + PartialEq + Default> Properties<T> {
impl<T: Copy + Default> Properties<T> {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn from_ptr_raw(mut p: *const T) -> Vec<T> {
pub fn from_ptr_raw(mut p: *const T) -> Vec<T>
where
T: PartialEq,
{
let mut res: Vec<T> = Vec::new();
if !p.is_null() {
@ -22,7 +25,10 @@ impl<T: Copy + PartialEq + Default> Properties<T> {
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn from_ptr(mut p: *const T) -> Option<Self> {
pub fn from_ptr(mut p: *const T) -> Option<Self>
where
T: PartialEq,
{
let mut res = Self::default();
if !p.is_null() {
@ -45,6 +51,16 @@ impl<T: Copy + PartialEq + Default> Properties<T> {
Some(res)
}
/// Returns true when there is no property available.
pub fn is_empty(&self) -> bool {
self.props.is_empty()
}
/// Returns the amount of key/value pairs available.
pub fn len(&self) -> usize {
self.props.len()
}
}
impl<T> Default for Properties<T> {