Commit | Line | Data |
---|---|---|
68362724 KS |
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 | ||
68362724 | 5 | use crate::bindings; |
69e6ce02 | 6 | use crate::status::VAError; |
68362724 KS |
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 { | |
69e6ce02 | 22 | type Error = VAError; |
68362724 KS |
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 | } | |
69e6ce02 | 40 | _other => Err(VAError::InvalidValue), |
68362724 KS |
41 | } |
42 | } | |
43 | } |