| 1 | // Copyright 2022 The ChromiumOS Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | use crate::bindings; |
| 6 | use crate::status::VAError; |
| 7 | |
| 8 | /// A wrapper over `VAGenericValue` giving us safe access to the underlying union members. |
| 9 | #[derive(Debug)] |
| 10 | pub enum GenericValue { |
| 11 | /// A wrapper over VAGenericValueTypeInteger |
| 12 | Integer(i32), |
| 13 | /// A wrapper over VAGenericValueTypeFloat |
| 14 | Float(f32), |
| 15 | /// A wrapper over VAGenericValueTypePointer |
| 16 | Pointer(*mut std::os::raw::c_void), |
| 17 | /// A wrapper over VAGenericValueTypeFunc |
| 18 | Func(bindings::VAGenericFunc), |
| 19 | } |
| 20 | |
| 21 | impl TryFrom<bindings::VAGenericValue> for GenericValue { |
| 22 | type Error = VAError; |
| 23 | |
| 24 | fn try_from(value: bindings::VAGenericValue) -> Result<Self, Self::Error> { |
| 25 | // Safe because we check the type before accessing the union. |
| 26 | match value.type_ { |
| 27 | // Safe because we check the type before accessing the union. |
| 28 | bindings::VAGenericValueType::VAGenericValueTypeInteger => { |
| 29 | Ok(Self::Integer(unsafe { value.value.i })) |
| 30 | } |
| 31 | bindings::VAGenericValueType::VAGenericValueTypeFloat => { |
| 32 | Ok(Self::Float(unsafe { value.value.f })) |
| 33 | } |
| 34 | bindings::VAGenericValueType::VAGenericValueTypePointer => { |
| 35 | Ok(Self::Pointer(unsafe { value.value.p })) |
| 36 | } |
| 37 | bindings::VAGenericValueType::VAGenericValueTypeFunc => { |
| 38 | Ok(Self::Func(unsafe { value.value.fn_ })) |
| 39 | } |
| 40 | _other => Err(VAError::InvalidValue), |
| 41 | } |
| 42 | } |
| 43 | } |