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

Commit ee132fac authored by Andrei Homescu's avatar Andrei Homescu
Browse files

binder_rs: Implement traits for Stability

This implements several traits for Stability: Debug, Copy, Clone,
all comparison traits, as well as conversion to/from i32 and
(de)serialization traits.

Bug: 169035750
Test: m
Change-Id: Icb7d625e9720ddb3b7fcbe0a069039571fb02273
parent cbfb18e1
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ use crate::sys;

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::ffi::{c_void, CStr, CString};
use std::fmt;
use std::fs::File;
@@ -70,6 +71,7 @@ pub trait Interface: Send + Sync {
/// An interface can promise to be a stable vendor interface ([`Vintf`]), or
/// makes no stability guarantees ([`Local`]). [`Local`] is
/// currently the default stability.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Stability {
    /// Default stability, visible to other modules in the same compilation
    /// context (e.g. modules on system.img)
@@ -85,6 +87,28 @@ impl Default for Stability {
    }
}

impl From<Stability> for i32 {
    fn from(stability: Stability) -> i32 {
        use Stability::*;
        match stability {
            Local => 0,
            Vintf => 1,
        }
    }
}

impl TryFrom<i32> for Stability {
    type Error = StatusCode;
    fn try_from(stability: i32) -> Result<Stability> {
        use Stability::*;
        match stability {
            0 => Ok(Local),
            1 => Ok(Vintf),
            _ => Err(StatusCode::BAD_VALUE)
        }
    }
}

/// A local service that can be remotable via Binder.
///
/// An object that implement this interface made be made into a Binder service
+14 −2
Original line number Diff line number Diff line
@@ -14,13 +14,13 @@
 * limitations under the License.
 */

use crate::binder::{AsNative, FromIBinder, Strong};
use crate::binder::{AsNative, FromIBinder, Stability, Strong};
use crate::error::{status_result, status_t, Result, Status, StatusCode};
use crate::parcel::Parcel;
use crate::proxy::SpIBinder;
use crate::sys;

use std::convert::TryInto;
use std::convert::{TryFrom, TryInto};
use std::ffi::c_void;
use std::os::raw::{c_char, c_ulong};
use std::mem::{self, MaybeUninit};
@@ -608,6 +608,18 @@ impl<T: DeserializeArray> DeserializeOption for Vec<T> {
    }
}

impl Serialize for Stability {
    fn serialize(&self, parcel: &mut Parcel) -> Result<()> {
        i32::from(*self).serialize(parcel)
    }
}

impl Deserialize for Stability {
    fn deserialize(parcel: &Parcel) -> Result<Self> {
        i32::deserialize(parcel).and_then(Stability::try_from)
    }
}

impl Serialize for Status {
    fn serialize(&self, parcel: &mut Parcel) -> Result<()> {
        unsafe {