float_pigment_css/sheet/
str_store.rs

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! String utilities for the binary format.

use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
use core::fmt::Debug;

use serde::{Deserialize, Serialize};

use super::*;
use inner::StrBufferInner;

#[cfg(all(not(feature = "std"), not(feature = "no-std-lock")))]
compile_error!("One of the `std` or `no-std-lock` features should be enabled");

// HACK
// Currently there is no global state for a ser/de call, but we need it to handle `StrRef` .
// To resolve this in a simple way, we use a thread-global state instead.
// Because there cannot be two ser/de in progressing in a single thread.
// However, this does not work on `no_std` env.
// So we just use a spin lock to prevent two ser/de in progressing in multiple `no_std` thread.
// This should be finally resolved by customizing ser/de process.
pub(super) enum SerdeThreadGlobalState {
    None,
    Ser(SerdeThreadGlobalStateSer),
    De(SerdeThreadGlobalStateDe),
    DePrepare {
        zero_copy: Box<dyn 'static + FnOnce()>,
    },
}

pub(super) struct SerdeThreadGlobalStateSer {
    str_buffer: Rc<StrBufferInner>,
    offset_gen: Vec<usize>,
    offsets: Option<alloc::vec::IntoIter<usize>>,
}

pub(super) struct SerdeThreadGlobalStateDe {
    str_buffer: Rc<StrBufferInner>,
    pub(super) zero_copy: Option<Box<dyn 'static + FnOnce()>>,
}

// It is safe because it will not be used across threads!
unsafe impl Send for SerdeThreadGlobalState {}
unsafe impl Sync for SerdeThreadGlobalState {}

impl SerdeThreadGlobalState {
    #[cfg(feature = "std")]
    #[allow(dead_code)]
    fn get<R>(f: impl FnOnce(&mut SerdeThreadGlobalState) -> R) -> R {
        thread_local! {
            static SERDE_THREAD_GLOBAL_STATE: RefCell<SerdeThreadGlobalState> = const { core::cell::RefCell::new(SerdeThreadGlobalState::None) };
        }
        SERDE_THREAD_GLOBAL_STATE.with(|x| {
            let mut x = x.borrow_mut();
            f(&mut x)
        })
    }

    #[cfg(all(not(feature = "std"), feature = "no-std-lock"))]
    #[allow(dead_code)]
    fn get<R>(f: impl FnOnce(&mut SerdeThreadGlobalState) -> R) -> R {
        static SERDE_THREAD_GLOBAL_STATE: spin::Lazy<spin::Mutex<SerdeThreadGlobalState>> =
            spin::Lazy::new(|| spin::Mutex::new(SerdeThreadGlobalState::None));
        f(&mut SERDE_THREAD_GLOBAL_STATE.lock())
    }

    #[allow(dead_code)]
    pub(super) fn ser<R>(ser: SerdeThreadGlobalStateSer, f: impl FnOnce() -> R) -> R {
        Self::get(|state| {
            let SerdeThreadGlobalState::None = state else {
                panic!("Invalid SerdeThreadGlobalState state");
            };
            *state = SerdeThreadGlobalState::Ser(ser);
        });
        let ret = f();
        Self::get(|state| {
            *state = SerdeThreadGlobalState::None;
        });
        ret
    }

    #[allow(dead_code)]
    pub(super) fn get_ser<R>(f: impl FnOnce(&mut SerdeThreadGlobalStateSer) -> R) -> R {
        Self::get(|state| {
            let SerdeThreadGlobalState::Ser(ser) = state else {
                panic!("Invalid SerdeThreadGlobalState state");
            };
            f(ser)
        })
    }

    #[allow(dead_code)]
    pub(super) fn de_prepare<R>(
        zero_copy: Box<dyn 'static + FnOnce()>,
        f: impl FnOnce() -> R,
    ) -> R {
        Self::get(|state| {
            let SerdeThreadGlobalState::None = state else {
                panic!("Invalid SerdeThreadGlobalState state");
            };
            *state = SerdeThreadGlobalState::DePrepare { zero_copy };
        });
        let ret = f();
        Self::get(|state| {
            *state = SerdeThreadGlobalState::None;
        });
        ret
    }

    #[allow(dead_code)]
    pub(super) fn de<R>(mut de: SerdeThreadGlobalStateDe, f: impl FnOnce() -> R) -> R {
        Self::get(|state| {
            let old_state = core::mem::replace(state, SerdeThreadGlobalState::None);
            de.zero_copy = match old_state {
                SerdeThreadGlobalState::None => None,
                SerdeThreadGlobalState::DePrepare { zero_copy } => Some(zero_copy),
                _ => panic!("Invalid SerdeThreadGlobalState state"),
            };
            *state = SerdeThreadGlobalState::De(de);
        });
        let ret = f();
        Self::get(|state| {
            *state = SerdeThreadGlobalState::None;
        });
        ret
    }

    #[allow(dead_code)]
    pub(super) fn get_de<R>(f: impl FnOnce(&mut SerdeThreadGlobalStateDe) -> R) -> R {
        Self::get(|state| {
            let SerdeThreadGlobalState::De(de) = state else {
                panic!("Invalid SerdeThreadGlobalState state");
            };
            f(de)
        })
    }

    #[allow(dead_code)]
    pub(super) fn get_de_optional<R>(
        f: impl FnOnce(Option<&mut SerdeThreadGlobalStateDe>) -> R,
    ) -> R {
        Self::get(|state| {
            if let SerdeThreadGlobalState::De(de) = state {
                f(Some(de))
            } else {
                f(None)
            }
        })
    }
}

pub(crate) fn str_buffer_de_env<R>(str_buffer: &StrBuffer, f: impl FnOnce() -> R) -> R {
    SerdeThreadGlobalState::de(
        SerdeThreadGlobalStateDe {
            str_buffer: str_buffer.inner.clone(),
            zero_copy: None,
        },
        f,
    )
}

pub(crate) fn str_buffer_ser_env<R, T>(
    first_gen_f: impl FnOnce() -> T,
    final_gen_f: impl FnOnce(T, StrBuffer) -> R,
) -> R {
    SerdeThreadGlobalState::ser(
        SerdeThreadGlobalStateSer {
            str_buffer: Rc::new(StrBufferInner::new()),
            offset_gen: vec![],
            offsets: None,
        },
        || {
            let r = first_gen_f();
            let buf = SerdeThreadGlobalState::get_ser(|state| {
                let buf = state.str_buffer.clone();
                let offset_gen = core::mem::take(&mut state.offset_gen);
                buf.freeze();
                state.offsets = Some(offset_gen.into_iter());
                buf
            });
            final_gen_f(r, StrBuffer { inner: buf })
        },
    )
}

pub(crate) mod inner {
    use alloc::{boxed::Box, vec::Vec};
    use core::cell::{Cell, UnsafeCell};

    pub(crate) struct StrBufferInner {
        writable: Cell<bool>,
        static_borrowed: Option<Box<dyn 'static + FnOnce()>>,
        buf: UnsafeCell<Vec<u8>>,
    }

    impl Drop for StrBufferInner {
        fn drop(&mut self) {
            if let Some(f) = self.static_borrowed.take() {
                let buf = unsafe { &mut *self.buf.get() };
                let mut empty = vec![];
                core::mem::swap(&mut empty, buf);
                let _ = Box::into_raw(empty.into_boxed_slice());
                f();
            }
        }
    }

    impl core::fmt::Debug for StrBufferInner {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            write!(
                f,
                "StrBufferInner {{ writable: {}, buf: [...] }}",
                self.writable.get()
            )
        }
    }

    impl StrBufferInner {
        pub(super) fn new() -> Self {
            Self {
                writable: Cell::new(true),
                static_borrowed: None,
                buf: UnsafeCell::new(vec![]),
            }
        }

        pub(super) fn new_with_buf(buf: Vec<u8>) -> Self {
            Self {
                writable: Cell::new(false),
                static_borrowed: None,
                buf: UnsafeCell::new(buf),
            }
        }

        pub(super) unsafe fn new_static_borrowed(
            buf: *mut [u8],
            drop_callback: Box<dyn 'static + FnOnce()>,
        ) -> Self {
            Self {
                writable: Cell::new(false),
                static_borrowed: Some(Box::new(drop_callback)),
                buf: UnsafeCell::new(Box::from_raw(buf).into_vec()),
            }
        }

        pub(super) fn freeze(&self) {
            self.writable.set(false);
        }

        pub(super) fn append(&self, s: &str) -> usize {
            if !self.writable.get() {
                panic!("StrBuffer is not in writable stage");
            }
            let buf = unsafe { &mut *self.buf.get() };
            let offset = buf.len();
            buf.append(&mut Vec::from(s.as_bytes()));
            offset
        }

        pub(super) fn read(&self) -> &[u8] {
            if self.writable.get() {
                panic!("StrBuffer is not in writable stage");
            }
            let buf = unsafe { &mut *self.buf.get() };
            buf.as_slice()
        }

        pub(super) fn len(&self) -> usize {
            let buf = unsafe { &mut *self.buf.get() };
            buf.len()
        }
    }
}

/// cbindgen:ignore
#[repr(C)]
#[derive(Debug, Clone)]
pub struct StrBuffer {
    inner: Rc<StrBufferInner>,
}

impl StrBuffer {
    #[cfg(feature = "serialize")]
    pub(crate) fn new() -> Self {
        Self {
            inner: Rc::new(StrBufferInner::new()),
        }
    }

    pub(crate) fn new_with_buf(buf: Vec<u8>) -> Self {
        Self {
            inner: Rc::new(StrBufferInner::new_with_buf(buf)),
        }
    }

    pub(crate) unsafe fn new_static_borrowed(
        buf: *mut [u8],
        drop_callback: Box<dyn 'static + FnOnce()>,
    ) -> Self {
        Self {
            inner: Rc::new(StrBufferInner::new_static_borrowed(buf, drop_callback)),
        }
    }

    #[cfg(feature = "serialize")]
    pub(crate) fn freeze(&mut self) {
        self.inner.freeze()
    }

    pub(crate) fn whole_buffer(&self) -> &[u8] {
        self.inner.read()
    }
}

/// An string format which is compatible with the binary format.
///
/// cbindgen:ignore
#[repr(C)]
#[derive(Clone)]
pub struct StrRef {
    offset: usize,
    len: usize,
    buf: Rc<StrBufferInner>,
}

impl StrRef {
    /// Convert it to `[u8]`.
    pub fn as_slice<'a>(&'a self) -> &'a [u8] {
        let buf = self.buf.read();
        unsafe {
            let ptr = (buf as *const [u8] as *const u8).add(self.offset);
            core::slice::from_raw_parts::<'a, u8>(ptr, self.len)
        }
    }

    /// Convert it to `str`.
    pub fn as_str(&self) -> &str {
        core::str::from_utf8(self.as_slice()).unwrap_or_default()
    }

    #[doc(hidden)]
    /// # Safety
    ///
    pub unsafe fn as_str_unchecked(&self) -> &str {
        core::str::from_utf8_unchecked(self.as_slice())
    }

    /// Convert it to `String`.
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(&self) -> String {
        String::from_utf8_lossy(self.as_slice()).into_owned()
    }

    /// Compare it with `str`.
    pub fn equal(&self, s: &str) -> bool {
        self.as_slice() == s.as_bytes()
    }

    #[doc(hidden)]
    #[cfg(feature = "ffi")]
    #[no_mangle]
    pub extern "C" fn str_ptr(&self) -> *const u8 {
        let buf = self.buf.read();
        unsafe { (buf as *const [u8] as *const u8).add(self.offset) }
    }

    #[doc(hidden)]
    #[cfg(feature = "ffi")]
    #[no_mangle]
    pub extern "C" fn str_len(&self) -> usize {
        self.len
    }
}

impl<T: alloc::string::ToString> From<T> for StrRef {
    fn from(s: T) -> Self {
        let s = s.to_string();
        let len = s.len();
        let buf = Rc::new(StrBufferInner::new_with_buf(s.into_bytes()));
        Self {
            offset: 0,
            len,
            buf,
        }
    }
}

impl Default for StrRef {
    fn default() -> Self {
        Self::from(String::new())
    }
}

impl PartialEq for StrRef {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl Serialize for StrRef {
    fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let offset = SerdeThreadGlobalState::get_ser(|state| {
            if let Some(offsets) = state.offsets.as_mut() {
                offsets.next().unwrap_or_default()
            } else {
                let x = state.str_buffer.append(self.as_str());
                state.offset_gen.push(x);
                0
            }
        });
        (offset, self.len).serialize(ser)
    }
}

impl<'de> Deserialize<'de> for StrRef {
    fn deserialize<D>(de: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let buf = SerdeThreadGlobalState::get_de(|state| state.str_buffer.clone());
        let (offset, len) = <(usize, usize)>::deserialize(de)?;
        let offset = offset.min(buf.len());
        Ok(Self { offset, len, buf })
    }
}
impl Debug for StrRef {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}
#[cfg(debug_assertions)]
impl crate::CompatibilityCheck for StrRef {
    fn check() {}
}