Skip to main content

mago_codex/
lib.rs

1use mago_atom::Atom;
2use mago_atom::atom;
3use mago_span::Span;
4
5pub mod assertion;
6pub mod consts;
7pub mod context;
8pub mod diff;
9pub mod differ;
10pub mod flags;
11pub mod identifier;
12pub mod issue;
13pub mod metadata;
14pub mod misc;
15pub mod populator;
16pub mod reference;
17pub mod scanner;
18pub mod signature;
19pub mod signature_builder;
20pub mod symbol;
21pub mod ttype;
22pub mod visibility;
23
24mod utils;
25
26#[must_use]
27pub fn get_anonymous_class_name(span: Span) -> Atom {
28    use std::io::Write;
29
30    // A 64-byte buffer on the stack. This is ample space for the prefix,
31    // u64 file id, and 2 u32 integers, preventing any chance of a heap allocation.
32    let mut buffer = [0u8; 64];
33
34    // Use a block to limit the scope of the mutable writer
35    // `writer` is a mutable slice that implements `std::io::Write`.
36    let mut writer = &mut buffer[..];
37
38    // SAFETY: We use `unwrap_unchecked` here because we are writing to a fixed-size buffer
39    unsafe {
40        write!(writer, "class@anonymous:{}-{}:{}", span.file_id, span.start.offset, span.end.offset).unwrap_unchecked();
41    };
42
43    // Determine how many bytes were written by checking the length of the original buffer
44    // against what the `writer` had left. This is a common pattern for `io::Write` on slices.
45    let written_len = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
46
47    atom(
48        // SAFETY: We use `unwrap_unchecked` here because we are certain the bytes
49        // up to `written_len` are valid UTF-8.
50        unsafe { std::str::from_utf8(&buffer[..written_len]).unwrap_unchecked() },
51    )
52}