4.1.7 更改 API

07-更改 API — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/android/aidl/example-service/changing-definition.html

4.1.7 更改 API

扩展一下 API:让客户端能为生日贺卡指定一系列行:

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

/** 生日服务接口。 */
interface IBirthdayService {
    /** 生成生日快乐消息。 */
    String wishHappyBirthday(String name, int years, in String[] text);
}

这会得到更新后的 IBirthdayService trait 定义:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0
trait IBirthdayService {
    fn wishHappyBirthday(
        &self,
        name: &str,
        years: i32,
        text: &[String],
    ) -> binder::Result<String>;
}
  • 注意 AIDL 定义中的 String[] 在 Rust 中被翻译为 &[String],即生成的绑定在可能的情况下会使用符合习惯的 Rust 类型:
    • in 数组参数翻译为切片。
    • out 与 inout 参数翻译为 &mut Vec<T>。
    • 返回值翻译为返回 Vec<T>。
最后修改 August 11, 2026: 更新 (70a5af133)