jlrs 0.23.0

jlrs provides bindings to the Julia C API that enable Julia code to be called from Rust and more.
Documentation
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
//! Dynamically and statically-sized frames.
//!
//! Every scope has its own frame which can hold some number of roots. When the scope ends these
//! roots are removed from the set of roots, so all data rooted in a frame can safely be used
//! until its scope ends. This hold true even if the frame is dropped before its scope ends.
//!
//! For more information see the documentation in the [`memory`] and [`target`] modules.
//!
//! [`memory`]: crate::memory
//! [`target`]: crate::memory::target

#[cfg(feature = "async")]
pub mod async_frame;

use std::{marker::PhantomData, pin::Pin, ptr::NonNull};

use jlrs_sys::{RawGcFrame, UnsizedGcFrame, pop_frame};

#[cfg(feature = "async")]
pub use self::async_frame::*;
use super::{
    ExtendedTarget, Target,
    output::Output,
    reusable_slot::ReusableSlot,
    slot_ref::{LocalSlotRef, StackSlotRef},
    unrooted::Unrooted,
};
use crate::{
    data::managed::Managed,
    memory::{context::stack::Stack, scope::private::LocalScopePriv},
    prelude::{LocalScope, Scope},
    private::Private,
};

/// A dynamically-sized frame that can hold an arbitrary number of roots.
pub struct GcFrame<'scope> {
    stack: &'scope Stack,
    offset: usize,
    _marker: PhantomData<&'scope mut &'scope ()>,
}

impl<'scope> GcFrame<'scope> {
    /// Returns a mutable reference to this frame.
    #[inline]
    pub fn as_mut(&mut self) -> &mut Self {
        self
    }

    /// Reserve capacity for at least `additional` roots.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.stack.reserve(additional)
    }

    /// Borrow the current frame.
    #[inline]
    pub fn borrow<'borrow>(&'borrow mut self) -> BorrowedFrame<'borrow, 'scope, Self> {
        BorrowedFrame(self, PhantomData)
    }

    /// Borrow this frame as an `ExtendedTarget` with the provided `target`.
    #[inline]
    pub fn extended_target<'target, 'borrow, Tgt>(
        &'borrow mut self,
        target: Tgt,
    ) -> ExtendedTarget<'target, 'scope, 'borrow, Tgt>
    where
        Tgt: Target<'target>,
    {
        ExtendedTarget {
            target,
            frame: self,
            _target_marker: PhantomData,
        }
    }

    /// Borrow this frame as an `ExtendedTarget` with an `Output` that targets this frame.
    #[inline]
    pub fn as_extended_target<'borrow>(
        &'borrow mut self,
    ) -> ExtendedTarget<'scope, 'scope, 'borrow, Output<'scope, StackSlotRef<'scope>>> {
        let target = self.output();
        ExtendedTarget {
            target,
            frame: self,
            _target_marker: PhantomData,
        }
    }

    /// Returns the number of values rooted in this frame.
    #[inline]
    pub fn n_roots(&self) -> usize {
        self.stack_size() - self.offset
    }

    /// Returns the number of values rooted in this frame.
    #[inline]
    pub fn stack_size(&self) -> usize {
        self.stack.size()
    }

    /// Returns an `Output` that targets the current frame.
    #[inline]
    pub fn output(&mut self) -> Output<'scope, StackSlotRef<'scope>> {
        unsafe {
            let offset = self.stack.reserve_slot();
            Output::new(StackSlotRef::new(self.stack, offset))
        }
    }

    /// Returns a `ReusableSlot` that targets the current frame.
    #[inline]
    pub fn reusable_slot(&mut self) -> ReusableSlot<'scope, StackSlotRef<'scope>> {
        unsafe {
            let offset = self.stack.reserve_slot();
            let slot = StackSlotRef::new(self.stack, offset);
            ReusableSlot::new(slot)
        }
    }

    /// Returns an `Unrooted` that targets the current frame.
    #[inline]
    pub const fn unrooted(&self) -> Unrooted<'scope> {
        unsafe { Unrooted::new() }
    }

    // Safety: ptr must be a valid pointer to T
    #[inline]
    #[track_caller]
    pub(crate) unsafe fn root<'data, T: Managed<'scope, 'data>>(
        &self,
        ptr: NonNull<T::Wraps>,
    ) -> T {
        unsafe {
            self.stack.push_root(ptr.cast());
            T::wrap_non_null(ptr, Private)
        }
    }

    #[inline]
    pub(crate) fn stack(&self) -> &Stack {
        self.stack
    }

    #[inline]
    pub(crate) unsafe fn nest<'nested>(&'nested mut self) -> (usize, GcFrame<'nested>) {
        let frame = GcFrame {
            stack: self.stack(),
            offset: self.stack.size(),
            _marker: PhantomData,
        };
        (self.stack.size(), frame)
    }

    // Safety: only one base frame can exist per `Stack`
    #[inline]
    pub(crate) unsafe fn base(stack: &'scope Stack) -> GcFrame<'scope> {
        debug_assert_eq!(stack.size(), 0);
        GcFrame {
            stack,
            offset: 0,
            _marker: PhantomData,
        }
    }

    // pub fn stack_addr(&self) -> *const c_void {
    //     self.stack as *const _ as *const _
    // }
}

unsafe impl<'f> Scope for GcFrame<'f> {
    #[inline]
    fn scope<T>(&mut self, func: impl for<'scope> FnOnce(GcFrame<'scope>) -> T) -> T {
        unsafe {
            let (offset, nested) = self.nest();
            let res = func(nested);
            self.stack.pop_roots(offset);
            res
        }
    }
}

/// A statically-sized frame that can hold `N` roots.
pub struct LocalGcFrame<'scope, const N: usize> {
    frame: &'scope PinnedLocalFrame<'scope, N>,
    offset: usize,
}

impl<'scope, const N: usize> LocalGcFrame<'scope, N> {
    /// Returns a mutable reference to this frame.
    #[inline]
    pub fn as_mut(&mut self) -> &mut Self {
        self
    }

    /// Returns the number of values rooted in this frame.
    #[inline]
    pub fn n_roots(&self) -> usize {
        self.offset
    }

    /// Returns the number of values that can be rooted in this frame.
    #[inline]
    pub const fn frame_size(&self) -> usize {
        N
    }

    /// Returns an `Output` that targets the current frame.
    #[inline]
    pub fn output(&mut self) -> Output<'scope, LocalSlotRef<'scope>> {
        unsafe {
            let slot = self.frame.frame.raw.get_root(self.offset);
            self.offset += 1;
            Output::new(LocalSlotRef::new(slot))
        }
    }

    /// Returns a `ReusableSlot` that targets the current frame.
    #[inline]
    pub fn reusable_slot(&mut self) -> ReusableSlot<'scope, LocalSlotRef<'scope>> {
        unsafe {
            let slot = self.frame.frame.raw.get_root(self.offset);
            let slot = LocalSlotRef::new(slot);
            self.offset += 1;
            ReusableSlot::new(slot)
        }
    }

    /// Returns a `Unrooted` that targets the current frame.
    #[inline]
    pub const fn unrooted(&self) -> Unrooted<'scope> {
        unsafe { Unrooted::new() }
    }

    #[inline]
    pub(crate) unsafe fn new(frame: &'scope PinnedLocalFrame<'scope, N>) -> Self {
        LocalGcFrame { frame, offset: 0 }
    }

    #[inline]
    #[track_caller]
    pub(crate) unsafe fn root<'data, T: Managed<'scope, 'data>>(
        &mut self,
        ptr: NonNull<T::Wraps>,
    ) -> T {
        unsafe {
            self.frame
                .frame
                .raw
                .set_root(self.offset, ptr.as_ptr().cast());
            self.offset += 1;
            T::wrap_non_null(ptr, Private)
        }
    }
}

pub struct UnsizedLocalGcFrame<'scope> {
    frame: UnsizedGcFrame<'scope>,
    offset: usize,
}

impl<'scope> UnsizedLocalGcFrame<'scope> {
    pub(crate) fn new(frame: UnsizedGcFrame<'scope>) -> Self {
        UnsizedLocalGcFrame { frame, offset: 0 }
    }

    /// Returns a mutable reference to this frame.
    #[inline]
    pub fn as_mut(&mut self) -> &mut Self {
        self
    }

    /// Returns the number of values rooted in this frame.
    #[inline]
    pub fn n_roots(&self) -> usize {
        self.offset
    }

    /// Returns the number of values that can be rooted in this frame.
    #[inline]
    pub fn frame_size(&self) -> usize {
        self.frame.size()
    }

    /// Returns an `Output` that targets the current frame.
    #[inline]
    pub fn output(&mut self) -> Output<'scope, LocalSlotRef<'scope>> {
        unsafe {
            let slot = self.frame.get_root(self.offset);
            self.offset += 1;
            Output::new(LocalSlotRef::new(slot))
        }
    }

    /// Returns a `ReusableSlot` that targets the current frame.
    #[inline]
    pub fn reusable_slot(&mut self) -> ReusableSlot<'scope, LocalSlotRef<'scope>> {
        unsafe {
            let slot = self.frame.get_root(self.offset);
            let slot = LocalSlotRef::new(slot);
            self.offset += 1;
            ReusableSlot::new(slot)
        }
    }

    /// Returns a `Unrooted` that targets the current frame.
    #[inline]
    pub const fn unrooted(&self) -> Unrooted<'scope> {
        unsafe { Unrooted::new() }
    }

    #[inline]
    #[track_caller]
    pub(crate) unsafe fn root<'data, T: Managed<'scope, 'data>>(
        &mut self,
        ptr: NonNull<T::Wraps>,
    ) -> T {
        unsafe {
            self.frame.get_root(self.offset).set(ptr.as_ptr().cast());
            self.offset += 1;
            T::wrap_non_null(ptr, Private)
        }
    }
}

/// A frame that has been borrowed. A new scope must be created before it can be used as a target
/// again.
// TODO privacy
pub struct BorrowedFrame<'borrow, 'current, F>(
    pub(crate) &'borrow mut F,
    pub(crate) PhantomData<&'current ()>,
);

impl<'borrow, 'current> LocalScopePriv for BorrowedFrame<'borrow, 'current, GcFrame<'current>> {}

unsafe impl<'borrow, 'current> LocalScope for BorrowedFrame<'borrow, 'current, GcFrame<'current>> {}

unsafe impl<'borrow, 'current> Scope for BorrowedFrame<'borrow, 'current, GcFrame<'current>> {
    #[inline]
    fn scope<T>(&mut self, func: impl for<'scope> FnOnce(GcFrame<'scope>) -> T) -> T {
        self.0.scope(func)
    }
}

#[cfg(feature = "async")]
impl<'borrow, 'current> LocalScopePriv
    for BorrowedFrame<'borrow, 'current, AsyncGcFrame<'current>>
{
}

#[cfg(feature = "async")]
unsafe impl<'borrow, 'current> LocalScope
    for BorrowedFrame<'borrow, 'current, AsyncGcFrame<'current>>
{
}

#[cfg(feature = "async")]
unsafe impl<'borrow, 'current> Scope for BorrowedFrame<'borrow, 'current, AsyncGcFrame<'current>> {
    #[inline]
    fn scope<T>(&mut self, func: impl for<'scope> FnOnce(GcFrame<'scope>) -> T) -> T {
        self.0.scope(func)
    }
}

#[cfg(feature = "async")]
unsafe impl<'borrow, 'current> crate::prelude::AsyncScope
    for BorrowedFrame<'borrow, 'current, AsyncGcFrame<'current>>
{
    #[inline]
    async fn async_scope<T>(
        &mut self,
        func: impl for<'scope> AsyncFnOnce(AsyncGcFrame<'scope>) -> T,
    ) -> T {
        self.0.async_scope(func).await
    }
}

#[repr(C)]
pub(crate) struct LocalFrame<const N: usize> {
    raw: RawGcFrame<N>,
}

impl<const N: usize> LocalFrame<N> {
    #[inline]
    pub(crate) const fn new() -> Self {
        unsafe {
            LocalFrame {
                raw: RawGcFrame::new(),
            }
        }
    }

    #[inline]
    pub(crate) unsafe fn pin<'scope>(&'scope mut self) -> PinnedLocalFrame<'scope, N> {
        unsafe { PinnedLocalFrame::new(self) }
    }
}

pub(crate) struct PinnedLocalFrame<'scope, const N: usize> {
    frame: Pin<&'scope mut LocalFrame<N>>,
    _marker: PhantomData<&'scope mut &'scope ()>,
}

impl<'scope, const N: usize> PinnedLocalFrame<'scope, N> {
    #[inline]
    unsafe fn new(frame: &'scope mut LocalFrame<N>) -> Self {
        unsafe {
            if N > 0 {
                frame.raw.push_frame();
            }

            PinnedLocalFrame {
                frame: Pin::new_unchecked(frame),
                _marker: PhantomData,
            }
        }
    }

    #[inline]
    pub(crate) unsafe fn pop(&self) {
        unsafe {
            if N > 0 {
                pop_frame()
            }
        }
    }
}