MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / create

Method create

aiscript-vm/src/string/interned.rs:83–106  ·  view source on GitHub ↗
(mc: &Mutation<'gc>, s: &[u8])

Source from the content-addressed store, hash-verified

81 // buffer. This can be improved when gc-arena learns to allocate variable sizes.
82
83 fn create<'gc, const N: usize>(mc: &Mutation<'gc>, s: &[u8]) -> InternedString<'gc> {
84 #[derive(Collect)]
85 #[collect(require_static)]
86 #[repr(C)]
87 struct InlineString<const N: usize> {
88 header: StringInner,
89 array: [u8; N],
90 }
91
92 assert!(s.len() <= N);
93 let mut string = InlineString {
94 header: StringInner {
95 hash: str_hash(s),
96 buffer: Buffer::Inline(s.len()),
97 },
98 array: [0; N],
99 };
100 string.array[0..s.len()].copy_from_slice(s);
101
102 let string = Gc::new(mc, string);
103 // SAFETY: We know we can cast to `StringInner` because `InlineString` is `#[repr(C)]`
104 // and `header` is the first field.
105 unsafe { InternedString(Gc::cast::<StringInner>(string)) }
106 }
107
108 let s = s.as_ref();
109

Callers 1

io_append_fileFunction · 0.80

Calls 3

str_hashFunction · 0.85
InternedStringClass · 0.85
lenMethod · 0.80

Tested by

no test coverage detected