Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8114cb75 authored by Andrei Homescu's avatar Andrei Homescu
Browse files

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
parent 897c6c67
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -22,29 +22,28 @@ use crate::binder::AsNative;
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 @@ impl DeserializeOption for ParcelFileDescriptor {
            // 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)))
        }
    }