Skip to content

Commit

Permalink
🐛 zv: Array::get now returns value at an index
Browse files Browse the repository at this point in the history
This makes it consistent with Dict::get and Maybe::get. The previous
impelmentation that gives you a `&[Value]` is now available as `inner`
method.

Fixes #263.
  • Loading branch information
zeenix committed Nov 28, 2023
1 parent 497bce3 commit fa51eea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
17 changes: 15 additions & 2 deletions zvariant/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,23 @@ impl<'a> Array<'a> {
}

/// Get all the elements.
pub fn get(&self) -> &[Value<'a>] {
pub fn inner(&self) -> &[Value<'a>] {
&self.elements
}

/// Get the value at the given index.
pub fn get<V>(&'a self, idx: usize) -> Result<Option<V>>
where
V: ?Sized + TryFrom<&'a Value<'a>>,
<V as TryFrom<&'a Value<'a>>>::Error: Into<crate::Error>,
{
self.elements
.get(idx)
.map(|v| v.downcast_ref::<V>())
.transpose()
.map_err(Into::into)
}

/// Get the number of elements.
pub fn len(&self) -> usize {
self.elements.len()
Expand Down Expand Up @@ -231,7 +244,7 @@ impl<'a> std::ops::Deref for Array<'a> {
type Target = [Value<'a>];

fn deref(&self) -> &Self::Target {
self.get()
self.inner()
}
}

Expand Down
29 changes: 13 additions & 16 deletions zvariant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ mod tests {
if let Value::Array(array) = v {
assert_eq!(*array.element_signature(), "y");
assert_eq!(array.len(), 2);
assert_eq!(array.get()[0], Value::U8(77));
assert_eq!(array.get()[1], Value::U8(88));
assert_eq!(array.get(0).unwrap(), Some(77u8));
assert_eq!(array.get(1).unwrap(), Some(88u8));
} else {
panic!();
}
Expand Down Expand Up @@ -781,8 +781,8 @@ mod tests {
if let Value::Array(array) = v {
assert_eq!(*array.element_signature(), "s");
assert_eq!(array.len(), 4);
assert_eq!(array.get()[0], Value::new("Hello"));
assert_eq!(array.get()[1], Value::new("World"));
assert_eq!(array[0], Value::new("Hello"));
assert_eq!(array[1], Value::new("World"));
} else {
panic!();
}
Expand Down Expand Up @@ -888,7 +888,7 @@ mod tests {
if let Value::Array(array) = v.try_clone().unwrap() {
assert_eq!(*array.element_signature(), "(yu(xbxas)s)");
assert_eq!(array.len(), 1);
let r = &array.get()[0];
let r = &array[0];
if let Value::Structure(r) = r {
let fields = r.fields();
assert_eq!(fields[0], Value::U8(u8::max_value()));
Expand All @@ -900,8 +900,8 @@ mod tests {
assert_eq!(fields[2], Value::I64(i64::max_value()));
if let Value::Array(as_) = &fields[3] {
assert_eq!(as_.len(), 2);
assert_eq!(as_.get()[0], Value::new("Hello"));
assert_eq!(as_.get()[1], Value::new("World"));
assert_eq!(as_[0], Value::new("Hello"));
assert_eq!(as_[1], Value::new("World"));
} else {
panic!();
}
Expand All @@ -928,7 +928,7 @@ mod tests {
if let Value::Array(array) = v {
assert_eq!(*array.element_signature(), "(yu(xbxas)s)");
assert_eq!(array.len(), 1);
let r = &array.get()[0];
let r = &array.get(0).unwrap().unwrap();
if let Value::Structure(r) = r {
let fields = r.fields();
assert_eq!(fields[0], Value::U8(u8::max_value()));
Expand All @@ -940,8 +940,8 @@ mod tests {
assert_eq!(fields[2], Value::I64(i64::max_value()));
if let Value::Array(as_) = &fields[3] {
assert_eq!(as_.len(), 2);
assert_eq!(as_.get()[0], Value::new("Hello"));
assert_eq!(as_.get()[1], Value::new("World"));
assert_eq!(as_.get(0).unwrap(), Some("Hello"));
assert_eq!(as_.get(1).unwrap(), Some("World"));
} else {
panic!();
}
Expand Down Expand Up @@ -1697,10 +1697,7 @@ mod tests {
Value::Maybe(maybe) => assert_eq!(maybe.get().unwrap(), mn),
#[cfg(feature = "option-as-array")]
Value::Array(array) => {
assert_eq!(
i16::try_from(array.get()[0].try_clone().unwrap()).unwrap(),
16i16
)
assert_eq!(i16::try_from(array[0].try_clone().unwrap()).unwrap(), 16i16)
}
_ => panic!("unexpected value {decoded:?}"),
}
Expand Down Expand Up @@ -1759,7 +1756,7 @@ mod tests {
match &v {
Value::Array(array) => {
assert_eq!(
String::try_from(array.get()[0].try_clone().unwrap()).unwrap(),
String::try_from(array[0].try_clone().unwrap()).unwrap(),
ms.unwrap()
)
}
Expand All @@ -1780,7 +1777,7 @@ mod tests {
#[cfg(feature = "option-as-array")]
Value::Array(array) => {
assert_eq!(
String::try_from(array.get()[0].try_clone().unwrap()).unwrap(),
String::try_from(array[0].try_clone().unwrap()).unwrap(),
ms.unwrap()
)
}
Expand Down

0 comments on commit fa51eea

Please sign in to comment.