libbinder_rs: Switch inner type of ParcelFileDescriptor
Change the inner type from File to OwnedFd to support
OSes that implement the latter but not the former.
An added benefit is that this brings the type closer
to the set of supported types that can be wrapped in
a ParcelFileDescriptors, e.g., sockets and memory buffers
in addition to files.
Bug: 242243245
Test: atest aidl_integration_test
Change-Id: I9ee14e479ab65c31459e6cecd736039deac1f29b
diff --git a/libs/binder/rust/src/parcel/file_descriptor.rs b/libs/binder/rust/src/parcel/file_descriptor.rs
index 5c688fa..6afe5ab 100644
--- a/libs/binder/rust/src/parcel/file_descriptor.rs
+++ b/libs/binder/rust/src/parcel/file_descriptor.rs
@@ -22,29 +22,28 @@
use crate::error::{status_result, Result, StatusCode};
use crate::sys;
-use std::fs::File;
-use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
+use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
/// Rust version of the Java class android.os.ParcelFileDescriptor
#[derive(Debug)]
-pub struct ParcelFileDescriptor(File);
+pub struct ParcelFileDescriptor(OwnedFd);
impl ParcelFileDescriptor {
/// Create a new `ParcelFileDescriptor`
- pub fn new(file: File) -> Self {
- Self(file)
+ pub fn new<F: Into<OwnedFd>>(fd: F) -> Self {
+ Self(fd.into())
}
}
-impl AsRef<File> for ParcelFileDescriptor {
- fn as_ref(&self) -> &File {
+impl AsRef<OwnedFd> for ParcelFileDescriptor {
+ fn as_ref(&self) -> &OwnedFd {
&self.0
}
}
-impl From<ParcelFileDescriptor> for File {
- fn from(file: ParcelFileDescriptor) -> File {
- file.0
+impl From<ParcelFileDescriptor> for OwnedFd {
+ fn from(fd: ParcelFileDescriptor) -> OwnedFd {
+ fd.0
}
}
@@ -120,7 +119,7 @@
// Safety: At this point, we know that the file descriptor was
// not -1, so must be a valid, owned file descriptor which we
// can safely turn into a `File`.
- let file = unsafe { File::from_raw_fd(fd) };
+ let file = unsafe { OwnedFd::from_raw_fd(fd) };
Ok(Some(ParcelFileDescriptor::new(file)))
}
}