Skip to main content

conjure_object/
log_safety.rs

1// Copyright 2026 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Type-level marking of values as safe to log.
15//!
16//! Enable the `log-safety` cargo feature to opt in.
17
18/// Marker trait for types whose serialized representation is safe to include
19/// in logs and error parameters.
20pub trait LogSafe {}
21
22/// Wrapper struct for known-safe data.
23///
24/// WARNING: This is an escape hatch which allows the developer (YOU) to log unsafe data.
25/// Misuse at your own risk.
26#[derive(serde::Serialize)]
27pub struct AssertLogSafe<T>(pub T);
28impl<T> LogSafe for AssertLogSafe<T> {}
29
30/// Marker bound that resolves to `LogSafe` when `log-safety` is enabled,
31/// or is satisfied by any type when it's not.
32#[cfg(feature = "log-safety")]
33pub trait MaybeLogSafe: LogSafe {}
34
35#[cfg(feature = "log-safety")]
36impl<T: LogSafe> MaybeLogSafe for T {}
37
38/// Marker bound that resolves to `LogSafe` when `log-safety` is enabled,
39/// or is satisfied by any type when it's not.
40#[cfg(not(feature = "log-safety"))]
41pub trait MaybeLogSafe {}
42
43#[cfg(not(feature = "log-safety"))]
44impl<T> MaybeLogSafe for T {}
45
46// conjure-object types that are safe
47impl LogSafe for crate::Uuid {}
48
49// containers
50impl<T: LogSafe + ?Sized> LogSafe for &T {}
51impl<T: LogSafe + ?Sized> LogSafe for &mut T {}
52impl<T: LogSafe + ?Sized> LogSafe for Box<T> {}
53impl<T: LogSafe + ?Sized> LogSafe for std::rc::Rc<T> {}
54impl<T: LogSafe + ?Sized> LogSafe for std::sync::Arc<T> {}
55impl<T: LogSafe> LogSafe for Option<T> {}
56impl<T: LogSafe> LogSafe for Vec<T> {}
57impl<T: LogSafe> LogSafe for std::collections::VecDeque<T> {}
58impl<T: LogSafe> LogSafe for std::collections::LinkedList<T> {}
59impl<T: LogSafe> LogSafe for [T] {}
60impl<T: LogSafe, const N: usize> LogSafe for [T; N] {}
61impl<K: LogSafe, V: LogSafe> LogSafe for std::collections::BTreeMap<K, V> {}
62impl<K: LogSafe, V: LogSafe, S> LogSafe for std::collections::HashMap<K, V, S> {}
63impl<T: LogSafe> LogSafe for std::collections::BTreeSet<T> {}
64impl<T: LogSafe, S> LogSafe for std::collections::HashSet<T, S> {}
65impl<T: LogSafe> LogSafe for std::collections::BinaryHeap<T> {}
66
67/// Re-exports LogSafe derive macro
68pub mod derive {
69    pub use conjure_macros::LogSafe;
70}