pw_format/format_string/platform.rs
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15//! Target platform integer and pointer width abstractions for `printf`
16//! formatting.
17//!
18//! When formatting decoded arguments (such as 64-bit varint-decoded integers
19//! from `pw_tokenizer`), `printf` conversion specifiers without an explicit
20//! 64-bit length modifier (`ll` or `j`) interpret values using the native type
21//! widths of the platform that produced the arguments.
22
23use super::FormatValueError;
24
25impl core::fmt::Display for FormatValueError {
26 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27 match self {
28 Self::MismatchedType => {
29 f.write_str("conversion specifier and argument types are incompatible")
30 }
31 Self::ValueOutOfRange => f.write_str("format argument value out of target range"),
32 }
33 }
34}
35
36impl core::error::Error for FormatValueError {}
37
38/// Trait implemented by target platform signed integer types (`i8`, `i16`,
39/// `i32`, `i64`).
40pub trait SignedInt: Copy + Into<i64> {
41 /// Truncates a 64-bit signed value to this type's width following C
42 /// integer conversion rules, and sign-extends the result back to `i64`.
43 fn truncate_i64(v: i64) -> i64;
44}
45
46impl SignedInt for i8 {
47 #[allow(clippy::cast_possible_truncation)]
48 fn truncate_i64(v: i64) -> i64 {
49 i64::from(v as i8)
50 }
51}
52
53impl SignedInt for i16 {
54 #[allow(clippy::cast_possible_truncation)]
55 fn truncate_i64(v: i64) -> i64 {
56 i64::from(v as i16)
57 }
58}
59
60impl SignedInt for i32 {
61 #[allow(clippy::cast_possible_truncation)]
62 fn truncate_i64(v: i64) -> i64 {
63 i64::from(v as i32)
64 }
65}
66
67impl SignedInt for i64 {
68 fn truncate_i64(v: i64) -> i64 {
69 v
70 }
71}
72
73/// Trait implemented by target platform unsigned integer types (`u8`, `u16`,
74/// `u32`, `u64`).
75pub trait UnsignedInt: Copy + Into<u64> {
76 /// Truncates a 64-bit unsigned value to this type's width following C
77 /// integer conversion rules, and zero-extends the result back to `u64`.
78 fn truncate_u64(v: u64) -> u64;
79}
80
81impl UnsignedInt for u8 {
82 #[allow(clippy::cast_possible_truncation)]
83 fn truncate_u64(v: u64) -> u64 {
84 u64::from(v as u8)
85 }
86}
87
88impl UnsignedInt for u16 {
89 #[allow(clippy::cast_possible_truncation)]
90 fn truncate_u64(v: u64) -> u64 {
91 u64::from(v as u16)
92 }
93}
94
95impl UnsignedInt for u32 {
96 #[allow(clippy::cast_possible_truncation)]
97 fn truncate_u64(v: u64) -> u64 {
98 u64::from(v as u32)
99 }
100}
101
102impl UnsignedInt for u64 {
103 fn truncate_u64(v: u64) -> u64 {
104 v
105 }
106}
107
108/// Trait implemented by target platform pointer-sized unsigned integer types
109/// (e.g., `u32` on ILP32, `u64` on LP64/LLP64).
110pub trait PointerInt: Copy + Into<u64> + TryFrom<u64> + TryFrom<usize> {
111 /// Converts a host `usize` pointer address to a `u64` target pointer value,
112 /// returning an error if the address exceeds the target platform's pointer
113 /// range.
114 ///
115 /// # Errors
116 ///
117 /// Returns [`FormatValueError::ValueOutOfRange`] if `v` exceeds the target
118 /// platform's pointer width.
119 fn ptr_from_usize(v: usize) -> Result<u64, FormatValueError> {
120 Self::try_from(v)
121 .map(Into::into)
122 .map_err(|_| FormatValueError::ValueOutOfRange)
123 }
124
125 /// Converts a 64-bit unsigned integer into the target platform's pointer
126 /// width.
127 fn ptr_from_u64(v: u64) -> u64;
128}
129
130impl PointerInt for u32 {
131 #[allow(clippy::cast_possible_truncation)]
132 fn ptr_from_u64(v: u64) -> u64 {
133 u64::from(v as u32)
134 }
135}
136
137impl PointerInt for u64 {
138 fn ptr_from_u64(v: u64) -> u64 {
139 v
140 }
141}
142
143/// Describes the native C/C++ integer and pointer types of the producing
144/// platform for all `printf` conversion specifiers and length modifiers.
145pub trait PlatformTypes {
146 /// Native `signed char` type (`hh` signed conversions, e.g., `i8`).
147 type SignedChar: SignedInt;
148 /// Native `unsigned char` type (`hh` unsigned conversions, e.g., `u8`).
149 type UnsignedChar: UnsignedInt;
150
151 /// Native `short` type (`h` signed conversions, e.g., `i16`).
152 type SignedShort: SignedInt;
153 /// Native `unsigned short` type (`h` unsigned conversions, e.g., `u16`).
154 type UnsignedShort: UnsignedInt;
155
156 /// Native `int` type (default signed conversions without length modifier,
157 /// e.g., `i32`).
158 type SignedInt: SignedInt;
159 /// Native `unsigned int` type (default unsigned conversions without length
160 /// modifier, e.g., `u32`).
161 type UnsignedInt: UnsignedInt;
162
163 /// Native `long` type (`l` signed conversions, e.g., `i32` on ILP32/LLP64,
164 /// `i64` on LP64).
165 type SignedLong: SignedInt;
166 /// Native `unsigned long` type (`l` unsigned conversions, e.g., `u32` on
167 /// ILP32/LLP64, `u64` on LP64).
168 type UnsignedLong: UnsignedInt;
169
170 /// Native `long long` type (`ll` signed conversions, e.g., `i64`).
171 type SignedLongLong: SignedInt;
172 /// Native `unsigned long long` type (`ll` unsigned conversions, e.g.,
173 /// `u64`).
174 type UnsignedLongLong: UnsignedInt;
175
176 /// Native `intmax_t` type (`j` signed conversions, e.g., `i64`).
177 type IntMax: SignedInt;
178 /// Native `uintmax_t` type (`j` unsigned conversions, e.g., `u64`).
179 type UIntMax: UnsignedInt;
180
181 /// Native signed `size_t` / `ssize_t` type (`z` signed conversions, e.g.,
182 /// `i32` on ILP32, `i64` on LP64/LLP64).
183 type SignedSize: SignedInt;
184 /// Native `size_t` type (`z` unsigned conversions, e.g., `u32` on ILP32,
185 /// `u64` on LP64/LLP64).
186 type Size: UnsignedInt;
187
188 /// Native `ptrdiff_t` type (`t` signed conversions, e.g., `i32` on ILP32,
189 /// `i64` on LP64/LLP64).
190 type PointerDiff: SignedInt;
191 /// Native unsigned `ptrdiff_t` type (`t` unsigned conversions, e.g., `u32`
192 /// on ILP32, `u64` on LP64/LLP64).
193 type UnsignedPointerDiff: UnsignedInt;
194
195 /// Native pointer integer type (`%p` conversions / `uintptr_t`, e.g., `u32`
196 /// on ILP32, `u64` on LP64/LLP64).
197 type Pointer: PointerInt;
198}
199
200/// Standard ILP32 platform types (32-bit `int`, `long`, `size_t`, `ptrdiff_t`,
201/// and 32-bit pointers; 64-bit `long long` and `intmax_t`), matching Pigweed's
202/// 32-bit embedded targets and default detokenizer conventions.
203#[derive(Clone, Copy, Debug, PartialEq, Eq)]
204pub struct Ilp32Platform;
205
206impl PlatformTypes for Ilp32Platform {
207 type SignedChar = i8;
208 type UnsignedChar = u8;
209 type SignedShort = i16;
210 type UnsignedShort = u16;
211 type SignedInt = i32;
212 type UnsignedInt = u32;
213 type SignedLong = i32;
214 type UnsignedLong = u32;
215 type SignedLongLong = i64;
216 type UnsignedLongLong = u64;
217 type IntMax = i64;
218 type UIntMax = u64;
219 type SignedSize = i32;
220 type Size = u32;
221 type PointerDiff = i32;
222 type UnsignedPointerDiff = u32;
223 type Pointer = u32;
224}
225
226/// Standard LP64 platform types (32-bit `int`; 64-bit `long`, `long long`,
227/// `intmax_t`, `size_t`, `ptrdiff_t`, and 64-bit pointers), matching 64-bit
228/// Unix/Linux/macOS targets.
229#[derive(Clone, Copy, Debug, PartialEq, Eq)]
230pub struct Lp64Platform;
231
232impl PlatformTypes for Lp64Platform {
233 type SignedChar = i8;
234 type UnsignedChar = u8;
235 type SignedShort = i16;
236 type UnsignedShort = u16;
237 type SignedInt = i32;
238 type UnsignedInt = u32;
239 type SignedLong = i64;
240 type UnsignedLong = u64;
241 type SignedLongLong = i64;
242 type UnsignedLongLong = u64;
243 type IntMax = i64;
244 type UIntMax = u64;
245 type SignedSize = i64;
246 type Size = u64;
247 type PointerDiff = i64;
248 type UnsignedPointerDiff = u64;
249 type Pointer = u64;
250}
251
252/// Standard LLP64 platform types (32-bit `int` and `long`; 64-bit `long long`,
253/// `intmax_t`, `size_t`, `ptrdiff_t`, and 64-bit pointers), matching 64-bit
254/// Windows targets.
255#[derive(Clone, Copy, Debug, PartialEq, Eq)]
256pub struct Llp64Platform;
257
258impl PlatformTypes for Llp64Platform {
259 type SignedChar = i8;
260 type UnsignedChar = u8;
261 type SignedShort = i16;
262 type UnsignedShort = u16;
263 type SignedInt = i32;
264 type UnsignedInt = u32;
265 type SignedLong = i32;
266 type UnsignedLong = u32;
267 type SignedLongLong = i64;
268 type UnsignedLongLong = u64;
269 type IntMax = i64;
270 type UIntMax = u64;
271 type SignedSize = i64;
272 type Size = u64;
273 type PointerDiff = i64;
274 type UnsignedPointerDiff = u64;
275 type Pointer = u64;
276}