]> Repositorios git - scryer-prolog.git/commitdiff
allocate empty partial strings
authorMark Thom <[email protected]>
Sun, 5 Apr 2020 08:55:30 +0000 (02:55 -0600)
committerMark Thom <[email protected]>
Sun, 5 Apr 2020 08:55:30 +0000 (02:55 -0600)
src/prolog/machine/partial_string.rs

index 0a74ecff7f84783b29ef7b0bdf161c8ff7edf90a..02e1cf34c395b369f38f27dc0d9398cf139a1c62 100644 (file)
@@ -111,11 +111,25 @@ impl PartialString {
     #[inline]
     pub(super)
     fn empty() -> Self {
-        PartialString {
-            buf: &"\u{0}".as_bytes()[0] as *const _,
-            len: '\u{0}'.len_utf8(),
+        let mut pstr = PartialString {
+            buf: ptr::null(),
+            len: 0,
             _marker: PhantomData,
+        };
+
+        unsafe {
+            let layout = alloc::Layout::from_size_align_unchecked(
+                '\u{0}'.len_utf8(),
+                mem::align_of::<u8>(),
+            );
+
+            pstr.buf = alloc::alloc(layout) as *const _;
+            pstr.len = '\u{0}'.len_utf8();
+
+            pstr.write_terminator_at(0);
         }
+
+        pstr
     }
 
     unsafe fn append_chars(mut self, src: &str) -> Option<(Self, &str)> {