fidl/marker.rs
1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Marker types supporting FIDL bindings.
6
7/// A marker for source-breaking patterns.
8///
9/// Generated FIDL bindings use this type to explicitly mark Rust types' usage
10/// pattern as source-breaking. That is, even if the marked type provides ABI
11/// guarantees handling fields marked with [`SourceBreaking`] means _probably_
12/// opting out of source compatibility guarantees provided by the Rust FIDL
13/// bindings.
14#[derive(Clone, Default, Debug, PartialEq, Eq)]
15pub struct SourceBreaking;
16
17/// A wrapper type whose [`Debug`](std::fmt::Debug) implementation formats the inner value wrapped in `"SENSITIVE{"` and `"}"`.
18///
19/// Generated FIDL bindings use this type to decorate fields marked with the
20/// `@sensitive` attribute in their [`Debug`](std::fmt::Debug) implementations.
21#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct SensitiveDebug<'a, T: ?Sized>(pub &'a T);
23
24impl<'a, T: ?Sized + std::fmt::Debug> std::fmt::Debug for SensitiveDebug<'a, T> {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "SENSITIVE{{{:?}}}", self.0)
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 #[test]
35 fn test_sensitive_debug() {
36 let val = 42;
37 assert_eq!(format!("{:?}", SensitiveDebug(&val)), "SENSITIVE{42}");
38 let str_val = "secret";
39 assert_eq!(format!("{:?}", SensitiveDebug(&str_val)), "SENSITIVE{\"secret\"}");
40 }
41}