Skip to content

Commit 5da35fa

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Add conversions between NativeHandle and AIDL NativeHandle." into main
2 parents 5103a38 + 9639e12 commit 5da35fa

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

libs/nativewindow/rust/Android.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ rust_defaults {
110110
name: "libnativewindow_defaults",
111111
srcs: ["src/lib.rs"],
112112
rustlibs: [
113+
"android.hardware.common-V2-rust",
113114
"libbinder_rs",
114115
"libbitflags",
115116
"libnativewindow_bindgen",

libs/nativewindow/rust/src/handle.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use android_hardware_common::{
16+
aidl::android::hardware::common::NativeHandle::NativeHandle as AidlNativeHandle,
17+
binder::ParcelFileDescriptor,
18+
};
1519
use std::{
1620
ffi::c_int,
1721
mem::forget,
@@ -190,6 +194,21 @@ impl Drop for NativeHandle {
190194
}
191195
}
192196

197+
impl From<AidlNativeHandle> for NativeHandle {
198+
fn from(aidl_native_handle: AidlNativeHandle) -> Self {
199+
let fds = aidl_native_handle.fds.into_iter().map(OwnedFd::from).collect();
200+
Self::new(fds, &aidl_native_handle.ints).unwrap()
201+
}
202+
}
203+
204+
impl From<NativeHandle> for AidlNativeHandle {
205+
fn from(native_handle: NativeHandle) -> Self {
206+
let ints = native_handle.ints().to_owned();
207+
let fds = native_handle.into_fds().into_iter().map(ParcelFileDescriptor::new).collect();
208+
Self { ints, fds }
209+
}
210+
}
211+
193212
// SAFETY: `NativeHandle` owns the `native_handle_t`, which just contains some integers and file
194213
// descriptors, which aren't tied to any particular thread.
195214
unsafe impl Send for NativeHandle {}
@@ -240,4 +259,20 @@ mod test {
240259

241260
drop(cloned);
242261
}
262+
263+
#[test]
264+
fn to_from_aidl() {
265+
let file = File::open("/dev/null").unwrap();
266+
let original = NativeHandle::new(vec![file.into()], &[42]).unwrap();
267+
assert_eq!(original.ints(), &[42]);
268+
assert_eq!(original.fds().len(), 1);
269+
270+
let aidl = AidlNativeHandle::from(original);
271+
assert_eq!(&aidl.ints, &[42]);
272+
assert_eq!(aidl.fds.len(), 1);
273+
274+
let converted_back = NativeHandle::from(aidl);
275+
assert_eq!(converted_back.ints(), &[42]);
276+
assert_eq!(converted_back.fds().len(), 1);
277+
}
243278
}

0 commit comments

Comments
 (0)