From: Ezekiel Newren Date: Mon, 08 Sep 2025 16:15:30 GMT Subject: Re: [PATCH 10/17] compat/rust_types.h: define rust primitive types Message-ID: In-Reply-To: On Mon, Sep 8, 2025 at 9:08 AM Junio C Hamano wrote: > The standard guarantees that these are all of the above are exactly > N-bits wide, so I can buy the above types. But before I can buy the > above typedefs, don't we need to rename existing variables that > squat on these names? > > $ git grep -n -E -e '\<[ui](8|16|32|64)\>' > > gives some hits, like > > reftable/record.c:678: uint8_t i64[8]; > t/helper/test-parse-options.c:123: uint16_t u16 = 0; > t/helper/test-parse-options.c:148: OPT_UNSIGNED(0, "u16", &u16, "get a 16 bit unsigned integer"), > > to avoid confusion? There are handful other hits. Those places should be cleaned up, but it's not an immediate problem because compat/rust_types.h is not auto included anywhere. These typedefs live in git-compat-util.h in my "Introduce Rust" patch series and I never had a problem with compilation or testing. One reason I included compat/posix.h instead of git-compat-util.h is because it includes stdbool.h where git-compat-util.h doesn't. I'll be happy to do that cleanup. > > +typedef float f32; > > +typedef double f64; > > It may be that they can be used interchangeably in practice on > popular platforms, but are these guaranteed to be equivalent by some > standard? C only cares about the minimum required range and > precision, so you may have allocated enough bytes thinking you can > fit a f32 but your float may not fit there. > > Or does Rust care only about platforms with IEEE 754 and would > refuse to port to other exotic architectures so the above worries > would not apply? If the typedefs in compat/rust_types.h are incorrect for a platform/target then compat/posix.h or compat/rust_types.h should be updated rather than relying on Rust's core::ffi's guess as what it is. On the Rust side: core::ffi assumes that a C 'float' is the same as f32 and that a C 'double' is an f64[1,2,3]. So I am making the same assumption that the Rust maintainers are _and_ I'm keeping ambiguity on the C side rather than eroding Rust's type precision. If a platform does not follow these assumptions then the compat/rust_types.h should warn those using that platform or fail to build entirely. [1] https://doc.rust-lang.org/1.89.0/core/ffi/type.c_float.html [2] https://doc.rust-lang.org/1.63.0/src/core/ffi/mod.rs.html#80-81 [3] https://doc.rust-lang.org/1.89.0/src/core/ffi/primitives.rs.html#34-35