4.2.4 Parcelable

04-Parcelable — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/android/aidl/types/parcelables.html

4.2.4 Parcelable

Rust 的 Binder 支持直接发送 parcelable:

birthday_service/aidl/com/example/birthdayservice/BirthdayInfo.aidl:

1
2
3
4
5
6
package com.example.birthdayservice;

parcelable BirthdayInfo {
    String name;
    int years;
}

birthday_service/aidl/com/example/birthdayservice/IBirthdayService.aidl:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import com.example.birthdayservice.BirthdayInfo;

interface IBirthdayService {
    /** 同样的事,但使用 parcelable。 */
    String wishWithInfo(in BirthdayInfo info);
    /** 同样的事,但使用 binder 对象。 */
    String wishWithProvider(IBirthdayInfoProvider provider);

    /** 同样的事,但使用 `IBinder`。 */
    String wishWithErasedProvider(IBinder provider);
}

birthday_service/src/client.rs:

1
2
3
4
5
6
7
8
9
// Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0
fn main() {
    binder::ProcessState::start_thread_pool();
    let service = connect().expect("Failed to connect to BirthdayService");

    let info = BirthdayInfo { name: "Alice".into(), years: 123 };
    service.wishWithInfo(&info)?;
}
最后修改 August 11, 2026: 更新 (70a5af133)