Skip to content

refactor: replace cstr! macros with c"..." literals #115

Closed

Description

@sunsided

Problem

cstr! is a local macro defined in 10 separate files (d_iwad.rs, dstrings.rs, d_mode.rs, r_draw.rs, w_main.rs, f_finale.rs, p_doors.rs, hu_stuff.rs, statdump.rs, p_spec.rs). It works around the absence of C-string literals, which were not stabilised until Rust 1.77.

Two problems:

  1. Misleading type: the macro casts as_ptr() (which yields *const u8) to *mut c_char. The returned pointer points into read-only static memory; writing through it is UB. The type should be *const c_char.
  2. Duplication: 10 identical or near-identical macro definitions across the codebase.

Evidence

d_main.rs already uses c"..." literals directly (e.g. c"PLAYPAL".as_ptr()), proving the approach is already established in the codebase. The project runs Rust 1.95, well past the 1.77 stabilisation point.

Fix

Replace every cstr!("foo") call with c"foo".as_ptr(), remove all macro_rules! cstr definitions, and update call sites that pass the result to a function expecting *mut c_char to use an explicit .cast_mut() only where the C API genuinely writes through the pointer. Read-only uses should stay *const c_char.

This is a mechanical find-and-replace with no behavioral change.

Scope

~10 files with macro definitions; usage spread across many more. Likely 50–100 call sites.

Metadata

Metadata

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions