Skip to content

Commit

Permalink
Merge pull request #54 from JustRustThings/add-integer-types-to-safea…
Browse files Browse the repository at this point in the history
…rray-to-vec

feat: handle all integer types in safearray_to_vec
  • Loading branch information
ohadravid authored Aug 14, 2022
2 parents 6b3bdfd + fae450e commit fb9ce19
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/safearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,48 @@ pub unsafe fn safe_array_to_vec(
arr: *mut SAFEARRAY,
item_type: u32,
) -> Result<Vec<Variant>, WMIError> {
let mut items = vec![];
fn copy_type_to_vec<T, F>(
arr: *mut SAFEARRAY,
variant_builder: F,
) -> Result<Vec<Variant>, WMIError>
where
T: Copy,
F: Fn(T) -> Variant,
{
let mut items = vec![];

let accessor = unsafe { SafeArrayAccessor::<T>::new(arr)? };

for item in accessor.as_slice().iter() {
items.push(variant_builder(*item));
}

match item_type {
VT_I4 => {
let accessor = unsafe { SafeArrayAccessor::<i32>::new(arr)? };
Ok(items)
}

for item in accessor.as_slice().iter() {
items.push(Variant::I4(*item))
}
}
match item_type {
VT_I1 => copy_type_to_vec(arr, Variant::I1),
VT_I2 => copy_type_to_vec(arr, Variant::I2),
VT_I4 => copy_type_to_vec(arr, Variant::I4),
VT_I8 => copy_type_to_vec(arr, Variant::I8),
VT_UI1 => copy_type_to_vec(arr, Variant::UI1),
VT_UI2 => copy_type_to_vec(arr, Variant::UI2),
VT_UI4 => copy_type_to_vec(arr, Variant::UI4),
VT_UI8 => copy_type_to_vec(arr, Variant::UI8),
VT_R4 => copy_type_to_vec(arr, Variant::R4),
VT_R8 => copy_type_to_vec(arr, Variant::R8),
VT_BSTR => {
let mut items = vec![];
let accessor = unsafe { SafeArrayAccessor::<BSTR>::new(arr)? };

for item_bstr in accessor.as_slice().iter() {
let item: &WideCStr = unsafe { WideCStr::from_ptr_str(*item_bstr) };

items.push(Variant::String(item.to_string()?));
}
Ok(items)
}
// TODO: Add support for all other types of arrays.
_ => return Err(WMIError::UnimplementedArrayItem),
_ => Err(WMIError::UnimplementedArrayItem),
}

Ok(items)
}

0 comments on commit fb9ce19

Please sign in to comment.