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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Named tuples to provide keyword arguments to functions.
//!
//! This module provides a custom `NamedTuple` type to aid in guaranteeing that a `NamedTuple` is
//! provided when necessary. A `NamedTuple` should be created with the [`named_tuple`] macro,
//! several functions and methods are provided to create named tuples in more specialized cases,
//! and create new instances that add and/or remove pairs.
//!
//! [`named_tuple`]: crate::named_tuple

use std::{fmt, marker::PhantomData, ptr::NonNull};

use fnv::FnvHashMap;
use jl_sys::{jl_field_index, jl_get_nth_field, jl_namedtuple_type, jl_value_t};
use smallvec::SmallVec;

use crate::{
    catch::{catch_exceptions, unwrap_exc},
    convert::to_symbol::ToSymbol,
    data::{
        layout::tuple::{NTuple, Tuple},
        managed::{Weak, private::ManagedPriv, type_name::TypeName, union_all::UnionAll},
        types::{construct_type::ConstructType, typecheck::Typecheck},
    },
    memory::{
        scope::LocalScopeExt,
        target::{TargetResult, TargetType, unrooted::Unrooted},
    },
    prelude::{DataType, Managed, Symbol, Target, Value, ValueData},
    private::Private,
};

/// Create a new named tuple.
///
/// The syntax is `named_tuple!(target, k1 => v1, k1 => v2, ...)`; the keys must implement
/// `ToSymbol`, the values must be `Value`s. An error is returned if the named tuple contains any
/// duplicate keys.
///
/// Example:
///
/// ```
/// # use jlrs::prelude::*;
/// # fn main() {
/// # let mut julia = Builder::new().start_local().unwrap();
/// julia
///     .local_scope::<JlrsResult<_>, 5>(|mut frame| unsafe {
///         // The code we evaluate is a simple function definition, which is safe.
///         let func = unsafe {
///             Value::eval_string(&mut frame, "func(; a=3, b=4, c=5) = a + b + c")? // 1
///         };
///
///         let a = Value::new(&mut frame, 1isize); // 2
///         let b = Value::new(&mut frame, 2isize); // 3
///         let nt = named_tuple!(&mut frame, "a" => a, "b" => b)?; // 4
///
///         // Call the previously defined function. This function simply sums its three
///         // keyword arguments and has no side effects, so it's safe to call.
///         let res = unsafe {
///             func.call_kw(&mut frame, [], nt)? // 5
///                 .unbox::<isize>()?
///         };
///
///         assert_eq!(res, 8);
///         Ok(())
///     }).unwrap();
/// # }
#[macro_export]
macro_rules! named_tuple {
    ($frame:expr_2021, $name:expr_2021 => $value:expr_2021) => {
        {
            let name = $crate::convert::to_symbol::ToSymbol::to_symbol(&$name, &$frame);
            $crate::data::managed::named_tuple::NamedTuple::from_n_pairs($frame, &[(name, $value)])
        }
    };
    ($frame:expr_2021, $name:expr_2021 => $value:expr_2021, $($rest:tt)+) => {
        {
            const N: usize = $crate::count!($($rest)+);
            let mut pairs: [::std::mem::MaybeUninit::<($crate::data::managed::symbol::Symbol, $crate::data::managed::value::Value)>; N] = [::std::mem::MaybeUninit::uninit(); N];
            let name = $crate::convert::to_symbol::ToSymbol::to_symbol(&$name, &$frame);

            pairs[0].write((name, $value));
            $crate::named_tuple!($frame, 1, &mut pairs, $($rest)+)
        }
    };
    ($frame:expr_2021, $i:expr_2021, $pairs:expr_2021, $name:expr_2021 => $value:expr_2021, $($rest:tt)+) => {
        {
            let name = $crate::convert::to_symbol::ToSymbol::to_symbol(&$name, &$frame);
            $pairs[$i].write((name, $value));
            $crate::named_tuple!($frame, $i + 1, $pairs, $($rest)+)
        }
    };
    ($frame:expr_2021, $i:expr_2021, $pairs:expr_2021, $name:expr_2021 => $value:expr_2021) => {
        {
            let name = $crate::convert::to_symbol::ToSymbol::to_symbol(&$name, &$frame);
            $pairs[$i].write((name, $value));

            let pairs: &[($crate::data::managed::symbol::Symbol, $crate::data::managed::value::Value); N] = unsafe {
                ::std::mem::transmute::<
                    &[::std::mem::MaybeUninit::<($crate::data::managed::symbol::Symbol, $crate::data::managed::value::Value)>; N],
                    &[($crate::data::managed::symbol::Symbol, $crate::data::managed::value::Value); N]
                >($pairs)
            };

            $crate::data::managed::named_tuple::NamedTuple::from_n_pairs($frame, pairs)
        }
    };
}

/// A `NamedTuple`.
///
/// Named tuples are mainly used as keyword arguments for Julia functions.
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct NamedTuple<'scope, 'data>(
    NonNull<jl_value_t>,
    PhantomData<&'scope ()>,
    PhantomData<&'data mut ()>,
);

impl<'scope, 'data> NamedTuple<'scope, 'data> {
    /// Create a new named tuple from `N` pairs of keywords and values.
    ///
    /// If an exception is thrown, e.g. due to duplicate keys, it is caught and returned.
    pub fn from_n_pairs<'target, Tgt, const N: usize>(
        target: Tgt,
        items: &[(Symbol<'_>, Value<'_, 'data>); N],
    ) -> NamedTupleResult<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let nt =
                match catch_exceptions(|| Self::from_n_pairs_unchecked(&target, items), unwrap_exc)
                {
                    Ok(nt) => Ok(nt.ptr()),
                    Err(e) => Err(e),
                };

            target.result_from_ptr(nt, Private)
        }
    }

    /// Create a new named tuple from `N` pairs of keywords and values without catching exceptions.
    ///
    /// Safety: if an exception is thrown, e.g. due to duplicate keys, it is not caught.
    pub unsafe fn from_n_pairs_unchecked<'target, Tgt, const N: usize>(
        target: Tgt,
        items: &[(Symbol<'_>, Value<'_, 'data>); N],
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        target.with_local_scope::<_, 3>(|target, mut frame| {
            let field_names = items.map(|(sym, _)| sym.as_value());
            let values = items.map(|(_, val)| val);
            let field_types = values.map(|val| val.datatype().as_value());

            unsafe {
                let names_tup = NTuple::<Symbol, N>::construct_type(&frame)
                    .as_value()
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&mut frame, &field_names);

                let field_types_tup = DataType::anytuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &field_types);

                UnionAll::namedtuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &[names_tup, field_types_tup])
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&frame, values)
                    .as_value()
                    .cast_unchecked::<NamedTuple>()
                    .root(target)
            }
        })
    }

    /// Create a new named tuple from an iterator of keywords and values.
    ///
    /// If an exception is thrown, e.g. due to duplicate keys, it is caught and returned.
    pub fn from_iter<'target, 'd, Tgt>(
        target: Tgt,
        items: impl ExactSizeIterator<Item = (Symbol<'d>, Value<'d, 'data>)>,
    ) -> NamedTupleResult<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let nt =
                match catch_exceptions(|| Self::from_iter_unchecked(&target, items), unwrap_exc) {
                    Ok(nt) => Ok(nt.ptr()),
                    Err(e) => Err(e),
                };

            target.result_from_ptr(nt, Private)
        }
    }

    /// Create a new named tuple from an iterator of keywords and values without catching
    /// exceptions.
    ///
    /// Safety: if an exception is thrown, e.g. due to duplicate keys, it is not caught.
    pub unsafe fn from_iter_unchecked<'target, 'd, Tgt>(
        target: Tgt,
        items: impl ExactSizeIterator<Item = (Symbol<'d>, Value<'d, 'data>)>,
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        target.with_local_scope::<_, 4>(|target, mut frame| {
            let n = items.len();
            let mut keys = SmallVec::<[_; 8]>::new();
            let mut values = SmallVec::<[_; 8]>::new();

            items.fold((&mut keys, &mut values), |(keys, values), (key, value)| {
                keys.push(key.as_value());
                values.push(value);
                (keys, values)
            });

            let field_types = values
                .iter()
                .copied()
                .map(|val| val.datatype().as_value())
                .collect::<SmallVec<[_; 8]>>();

            unsafe {
                let mut syms = SmallVec::<[_; 8]>::with_capacity(n);
                let st = DataType::symbol_type(&frame).as_value();
                for _ in 0..n {
                    syms.push(st);
                }

                let names_tup = DataType::anytuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, syms)
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&mut frame, &keys);

                let field_types_tup = DataType::anytuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &field_types);

                UnionAll::namedtuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &[names_tup, field_types_tup])
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&frame, values)
                    .as_value()
                    .cast_unchecked::<NamedTuple>()
                    .root(target)
            }
        })
    }

    /// Create a new named tuple from `keys` and `values`.
    ///
    /// There must be as many keys as values or this function will panic.
    ///
    /// If an exception is thrown, e.g. due to duplicate keys, it is caught and returned.
    pub fn new<'target, Tgt>(
        target: Tgt,
        keys: &[Symbol],
        values: &[Value<'_, 'data>],
    ) -> NamedTupleResult<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        unsafe {
            let nt =
                match catch_exceptions(|| Self::new_unchecked(&target, keys, values), unwrap_exc) {
                    Ok(nt) => Ok(nt.ptr()),
                    Err(e) => Err(e),
                };

            target.result_from_ptr(nt, Private)
        }
    }

    /// Create a new named tuple from `keys` and `values` without catching exceptions.
    ///
    /// There must be as many keys as values or this function will panic.
    ///
    /// Safety: if an exception is thrown, e.g. due to duplicate keys, it is not caught.
    pub unsafe fn new_unchecked<'target, Tgt>(
        target: Tgt,
        keys: &[Symbol],
        values: &[Value<'_, 'data>],
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let n = keys.len();
        assert_eq!(n, values.len(), "mismatched number of keys and values");

        target.with_local_scope::<_, 4>(|target, mut frame| {
            let field_types = values
                .iter()
                .copied()
                .map(|val| val.datatype().as_value())
                .collect::<Vec<_>>();

            unsafe {
                let mut syms = SmallVec::<[_; 8]>::with_capacity(n);
                let st = DataType::symbol_type(&frame).as_value();
                for _ in 0..n {
                    syms.push(st);
                }

                let keys_v = std::slice::from_raw_parts(keys.as_ptr().cast(), n);

                let names_tup = DataType::anytuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, syms)
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&mut frame, keys_v);

                let field_types_tup = DataType::anytuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &field_types);

                UnionAll::namedtuple_type(&frame)
                    .as_value()
                    .apply_type_unchecked(&mut frame, &[names_tup, field_types_tup])
                    .cast_unchecked::<DataType>()
                    .instantiate_unchecked_priv(&frame, values)
                    .as_value()
                    .cast_unchecked::<NamedTuple>()
                    .root(target)
            }
        })
    }

    /// Returns the field names of this named tuple.
    ///
    /// The field names of a type are normally accessed through its [`DataType`], because
    /// they are encoded differently for `NamedTuple`s this custom method must be used instead.
    pub fn field_names(self) -> &'scope [Symbol<'scope>] {
        let dt = self.as_value().datatype();

        let names_param = dt.parameter(0);
        if names_param.is_none() {
            return &[];
        }

        let names_param = names_param.unwrap();
        if !names_param.is::<Tuple>() {
            return &[];
        }

        let sz = names_param.datatype().size().unwrap() as usize / std::mem::size_of::<Symbol>();
        let names = names_param.unwrap(Private).cast::<Symbol>();
        unsafe { std::slice::from_raw_parts(names, sz) }
    }

    /// Returns `true` if this named tuple contains `keyword`.
    pub fn contains<K: ToSymbol>(self, keyword: K) -> bool {
        let dt = self.as_value().datatype();

        unsafe {
            let sym = keyword.to_symbol_priv(Private);
            jl_field_index(dt.unwrap(Private), sym.unwrap(Private), 0) >= 0
        }
    }

    /// Returns the value associated with `keyword` if it is present in this named tuple.
    pub fn get<'target, K, Tgt>(
        self,
        target: Tgt,
        keyword: K,
    ) -> Option<ValueData<'target, 'data, Tgt>>
    where
        K: ToSymbol,
        Tgt: Target<'target>,
    {
        let dt = self.as_value().datatype();

        unsafe {
            let sym = keyword.to_symbol_priv(Private);
            let idx = jl_field_index(dt.unwrap(Private), sym.unwrap(Private), 0);
            if idx < 0 {
                return None;
            }

            let val = jl_get_nth_field(self.as_value().unwrap(Private), idx as usize);
            Some(Value::wrap_non_null(NonNull::new(val)?, Private).root(target))
        }
    }

    /// Creates a new named tuple from `self` where `key` has been removed.
    pub fn remove<'target, Tgt>(
        self,
        target: Tgt,
        key: Symbol,
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        self.filter(target, &[key])
    }

    /// Creates a new named tuple from `self` where `key` is set to `value`.
    pub fn set<'target, Tgt>(
        self,
        target: Tgt,
        key: Symbol<'_>,
        value: Value<'_, 'data>,
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        self.extend_iter(target, [(key, value)].into_iter())
    }

    /// Creates a new named tuple from `self`, excluding any keyword present in `remove`.
    pub fn filter<'target, Tgt>(
        self,
        target: Tgt,
        remove: &[Symbol],
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let n_roots = self.as_value().n_fields();
        let fnames = self.field_names();
        target.with_unsized_local_scope(n_roots, |target, mut frame| {
            let mut keys = Vec::with_capacity(n_roots - remove.len());
            let mut values = Vec::with_capacity(n_roots - remove.len());

            for key in fnames.iter().copied() {
                if !remove.contains(&key) {
                    let value = self.as_value().get_field(&mut frame, key).unwrap();

                    keys.push(key);
                    values.push(value);
                }
            }

            // Safety: there cannot be any duplicates
            unsafe { Self::new_unchecked(target, keys.as_ref(), values.as_ref()) }
        })
    }

    /// Creates a new named tuple from `self`, and adds additional pairs from `keys` and `values`.
    /// New values override the old ones.
    ///
    /// This method panics if the number of elements in `keys` and `values` don't match.
    pub fn extend<'target, Tgt>(
        self,
        target: Tgt,
        keys: &[Symbol],
        values: &[Value<'_, 'data>],
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let n = keys.len();
        assert_eq!(n, values.len(), "mismatched number of keys and values");

        let nt = self.as_value();
        let n_fields = nt.n_fields();

        let field_names = self.field_names();

        target.with_unsized_local_scope(n_fields, |target, mut frame| {
            let mut map = FnvHashMap::default();

            unsafe {
                for i in 0..n_fields {
                    let key = field_names[i];
                    let value = nt.get_nth_field(&mut frame, i).unwrap();
                    map.insert(key, value);
                }

                for (key, value) in keys.iter().copied().zip(values.iter().copied()) {
                    map.insert(key, value);
                }

                // There cannot be any duplicates
                Self::from_iter_unchecked(target, map.iter().map(|(a, b)| (*a, *b)))
            }
        })
    }

    /// Create a new named tuple from this one and an iterator of key-value pairs. New values
    /// override the old ones.
    pub fn extend_iter<'target, 'd, Tgt>(
        self,
        target: Tgt,
        items: impl Iterator<Item = (Symbol<'d>, Value<'d, 'data>)>,
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let nt = self.as_value();
        let n_fields = nt.n_fields();

        let field_names = self.field_names();

        target.with_unsized_local_scope(n_fields, |target, mut frame| {
            let mut map = FnvHashMap::default();

            unsafe {
                for i in 0..n_fields {
                    let key = field_names[i];
                    let value = nt.get_nth_field(&mut frame, i).unwrap();
                    map.insert(key, value);
                }

                for (key, value) in items {
                    map.insert(key, value);
                }

                // There cannot be any duplicates
                Self::from_iter_unchecked(target, map.iter().map(|(a, b)| (*a, *b)))
            }
        })
    }

    /// Combines [`Self::filter`] and [`Self::extend`]. All duplicates are removed, later elements
    /// win.
    ///
    /// This method panics if the number of elements in `keys` and `values` don't match.
    pub fn filter_extend<'target, Tgt>(
        self,
        target: Tgt,
        remove: &[Symbol],
        keys: &[Symbol],
        values: &[Value<'_, 'data>],
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let n = keys.len();
        assert_eq!(n, values.len(), "mismatched number of keys and values");

        let field_names = self.field_names();
        let n_fields = field_names.len();

        target.with_unsized_local_scope(n_fields, |target, mut frame| {
            let mut retained_keys = Vec::with_capacity(n_fields - remove.len());
            let mut retained_values = Vec::with_capacity(n_fields - remove.len());

            for key in field_names.iter().copied() {
                if !remove.contains(&key) {
                    let value = self
                        .as_value()
                        .get_field(&mut frame, key)
                        .expect("missing field");

                    retained_keys.push(key);
                    retained_values.push(value);
                }
            }

            let mut map = FnvHashMap::default();
            for (key, value) in retained_keys
                .iter()
                .copied()
                .zip(retained_values.iter().copied())
            {
                map.insert(key, value);
            }
            for (key, value) in keys.iter().copied().zip(values.iter().copied()) {
                map.insert(key, value);
            }

            unsafe { Self::from_iter_unchecked(target, map.iter().map(|(a, b)| (*a, *b))) }
        })
    }

    /// Combines [`Self::filter`] and [`Self::extend_iter`]. All duplicates are removed, later
    /// pairs win.
    pub fn filter_extend_iter<'target, 'd, Tgt>(
        self,
        target: Tgt,
        remove: &[Symbol],
        items: impl ExactSizeIterator<Item = (Symbol<'d>, Value<'d, 'data>)>,
    ) -> NamedTupleData<'target, 'data, Tgt>
    where
        Tgt: Target<'target>,
    {
        let field_names = self.field_names();
        let n_roots = field_names.len();

        target.with_unsized_local_scope(n_roots, |target, mut frame| {
            let mut retained_keys = Vec::with_capacity(n_roots - remove.len());
            let mut retained_values = Vec::with_capacity(n_roots - remove.len());

            for key in field_names.iter().copied() {
                if !remove.contains(&key) {
                    let value = self
                        .as_value()
                        .get_field(&mut frame, key)
                        .expect("missing field");

                    retained_keys.push(key);
                    retained_values.push(value);
                }
            }

            let mut map = FnvHashMap::default();
            for (key, value) in retained_keys
                .iter()
                .copied()
                .zip(retained_values.iter().copied())
            {
                map.insert(key, value);
            }
            for (key, value) in items {
                map.insert(key, value);
            }

            unsafe { Self::from_iter_unchecked(target, map.iter().map(|(a, b)| (*a, *b))) }
        })
    }
}

impl fmt::Debug for NamedTuple<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_value().fmt(f)
    }
}

impl<'scope, 'data> ManagedPriv<'scope, 'data> for NamedTuple<'scope, 'data> {
    type Wraps = jl_value_t;
    type WithLifetimes<'target, 'da> = NamedTuple<'target, 'da>;
    const NAME: &'static str = "NamedTuple";

    // Safety: `inner` must not have been freed yet, the result must never be
    // used after the GC might have freed it.
    #[inline]
    unsafe fn wrap_non_null(inner: NonNull<Self::Wraps>, _: Private) -> Self {
        Self(
            inner,
            ::std::marker::PhantomData,
            ::std::marker::PhantomData,
        )
    }

    #[inline]
    fn unwrap_non_null(self, _: Private) -> NonNull<Self::Wraps> {
        self.0
    }
}

unsafe impl Typecheck for NamedTuple<'_, '_> {
    #[inline]
    fn typecheck(t: DataType) -> bool {
        unsafe { t.type_name() == TypeName::of_namedtuple(&Unrooted::new()) }
    }
}

/// A [`NamedTuple`] that has not been explicitly rooted.
pub type WeakNamedTuple<'scope, 'data> = Weak<'scope, 'data, NamedTuple<'scope, 'data>>;

/// A [`WeakNamedTuple`] with static lifetimes. This is a useful shorthand for signatures of
/// `ccall`able functions that return a [`NamedTuple`].
pub type NamedTupleRet = WeakNamedTuple<'static, 'static>;

/// `NamedTuple` or `WeakNamedTuple`, depending on the target type `Tgt`.
pub type NamedTupleData<'target, 'data, Tgt> =
    <Tgt as TargetType<'target>>::Data<'data, NamedTuple<'target, 'data>>;

/// `JuliaResult<NamedTuple>` or `WeakJuliaResult<WeakNamedTuple>`, depending on the target type `Tgt`.
pub type NamedTupleResult<'target, 'data, Tgt> =
    TargetResult<'target, 'data, NamedTuple<'target, 'data>, Tgt>;

impl_construct_type_managed!(NamedTuple, 2, jl_namedtuple_type);
impl_ccall_arg_managed!(NamedTuple, 2);