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