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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! All traits used by the public API of this crate.
//!
//! A quick note: all of these traits are sealed, you can't implement them on your own types. Most
//! serve as marker traits that are used as trait bounds on some methods to enforce you can only
//! do things that make sense. The one trait that you will use directly is the [`Frame`] trait
//! which exposes methods that let you create [`Output`]s in the current frame and nest frames.
//! This trait is automatically brought into scope by using the [`prelude`] module.
//!
//! [`Frame`]: trait.Frame.html
//! [`Output`]: ../frame/struct.Output.html
//! [`prelude`]: ../prelude/index.html

use crate::array::Array;
use crate::error::{AllocError, JlrsResult};
use crate::frame::{DynamicFrame, Output, StaticFrame};
use crate::symbol::Symbol;
use std::borrow::Cow;

// All these traits have a public and a private side. In order to prevent users from using methods
// that are for internal use only, all traits extend a base trait with the same name from the
// crate-public private module that contains the methods intended for private use and serves as a
// sealing trait. In order to retain information about what types implement a trait in the docs,
// the public trait is explicitly implemented for each type that implements the associated private
// trait rather than with a single blanket implementation.
macro_rules! p {
    ($trait:ident, $type:ty, $($bounds:tt)+) => {
        impl<$($bounds)+> $trait for $type {}
    };
    ($trait:ident, $type:ty) => {
        impl $trait for $type {}
    };
}

/// Trait implemented by types that can be converted to a temporary `Symbol`.
pub trait TemporarySymbol: private::TemporarySymbol {}

/// Trait implemented by types that can be converted to a Julia value.
pub trait IntoJulia: private::IntoJulia {}

/// Trait implemented by types that have an associated type in Julia.
pub trait JuliaType: private::JuliaType {}

/// Trait implemented by types that have the same representation in Julia and Rust when they are
/// used as array data. Arrays whose elements are of a type that implements this trait can share
/// their contents between Julia and Rust. This includes all types that implement `JuliaType`
/// except `bool` and `char`.
pub trait ArrayDatatype: private::ArrayDatatype + JuliaType {}

/// Trait implemented by types that can be created from a Julia value.
pub trait TryUnbox: private::TryUnbox {}

/// Functionality shared by [`StaticFrame`] and [`DynamicFrame`]. These structs let you protect
/// data from garbage collection. The lifetime of a frame is assigned to the values and outputs
/// that are created using that frame. After a frame is dropped, these items are no longer 
/// protected and cannot be used.
///
/// If you need the result of a function call to be valid outside the frame where it is called,
/// you can call `Frame::output` to create an [`Output`] and call the function through
/// [`Value::call_output`] or one of the other `call*_output` methods. The result will share the
/// output's lifetime so it can be used until the output's frame goes out of scope.
///
/// [`StaticFrame`]: ../frame/struct.StaticFrame.html
/// [`DynamicFrame`]: ../frame/struct.DynamicFrame.html
/// [`Module`]: ../module/struct.Module.html
/// [`Julia::frame`]: ../struct.Julia.html#method.frame
/// [`Julia::dynamic_frame`]: ../struct.Julia.html#method.dynamic_frame
/// [`Output`]: ../frame/struct.Output.html
/// [`Value::call_output`]: ../value/struct.Value.html#method.call_output
pub trait Frame<'frame>: private::Frame<'frame> {
    /// Create a `StaticFrame` that can hold `capacity` values, and call the given closure.
    /// Returns the result of this closure, or an error if the new frame can't be created
    /// because there's not enough space on the GC stack. The number of required slots on the
    /// stack is `capacity + 2`.
    ///
    /// Returns an error if there is not enough space on the stack.
    fn frame<'nested, T, F: FnOnce(&mut StaticFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        capacity: usize,
        func: F,
    ) -> JlrsResult<T>;

    /// Create a `DynamicFrame` and call the given closure.  Returns the result of this closure,
    /// or an error if the new frame can't be created because the stack is too small. The number
    /// of required slots on the stack is `2`.
    ///
    /// Returns an error if there is not enough space on the stack.
    fn dynamic_frame<'nested, T, F: FnOnce(&mut DynamicFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        func: F,
    ) -> JlrsResult<T>;

    /// Returns a new `Output`, this takes one slot on the GC stack. A function that uses this
    /// output will not use a slot on the GC stack, but the one associated with this output. This
    /// extends the lifetime of that value to be valid until the frame that created the output
    /// goes out of scope.
    ///
    /// Returns an error if there is not enough space on the stack.
    fn output(&mut self) -> JlrsResult<Output<'frame>>;

    /// Returns the number of values belonging to this frame.
    fn size(&self) -> usize;

    #[doc(hidden)]
    // Exists for debugging purposes, prints the contents of the GC stack.
    fn print_memory(&self);
}

p!(TemporarySymbol, String);
p!(TemporarySymbol, &dyn AsRef<str>);
p!(TemporarySymbol, &'a str, 'a);
p!(TemporarySymbol, Cow<'a, str>, 'a);
p!(TemporarySymbol, Symbol<'s>, 's);

p!(IntoJulia, bool);
p!(IntoJulia, char);
p!(IntoJulia, u8);
p!(IntoJulia, u16);
p!(IntoJulia, u32);
p!(IntoJulia, u64);
p!(IntoJulia, i8);
p!(IntoJulia, i16);
p!(IntoJulia, i32);
p!(IntoJulia, i64);
p!(IntoJulia, f32);
p!(IntoJulia, f64);
p!(IntoJulia, usize);
p!(IntoJulia, isize);
p!(IntoJulia, String);
p!(IntoJulia, &dyn AsRef<str>);
p!(IntoJulia, &'a str, 'a);
p!(IntoJulia, Cow<'a, str>, 'a);

p!(JuliaType, bool);
p!(JuliaType, char);
p!(JuliaType, u8);
p!(JuliaType, u16);
p!(JuliaType, u32);
p!(JuliaType, u64);
p!(JuliaType, i8);
p!(JuliaType, i16);
p!(JuliaType, i32);
p!(JuliaType, i64);
p!(JuliaType, f32);
p!(JuliaType, f64);
p!(JuliaType, usize);
p!(JuliaType, isize);

p!(ArrayDatatype, u8);
p!(ArrayDatatype, u16);
p!(ArrayDatatype, u32);
p!(ArrayDatatype, u64);
p!(ArrayDatatype, i8);
p!(ArrayDatatype, i16);
p!(ArrayDatatype, i32);
p!(ArrayDatatype, i64);
p!(ArrayDatatype, f32);
p!(ArrayDatatype, f64);
p!(ArrayDatatype, usize);
p!(ArrayDatatype, isize);

p!(TryUnbox, bool);
p!(TryUnbox, char);
p!(TryUnbox, u8);
p!(TryUnbox, u16);
p!(TryUnbox, u32);
p!(TryUnbox, u64);
p!(TryUnbox, i8);
p!(TryUnbox, i16);
p!(TryUnbox, i32);
p!(TryUnbox, i64);
p!(TryUnbox, f32);
p!(TryUnbox, f64);
p!(TryUnbox, usize);
p!(TryUnbox, isize);
p!(TryUnbox, String);
p!(TryUnbox, Array<A>, A: ArrayDatatype);

impl<'frame> Frame<'frame> for StaticFrame<'frame> {
    fn frame<'nested, T, F: FnOnce(&mut StaticFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        capacity: usize,
        func: F,
    ) -> JlrsResult<T> {
        let mut frame = unsafe { self.nested_frame(capacity).unwrap() };
        func(&mut frame)
    }

    fn dynamic_frame<'nested, T, F: FnOnce(&mut DynamicFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        func: F,
    ) -> JlrsResult<T> {
        unsafe {
            let mut view = self.memory.nest_dynamic();
            let idx = view.new_frame()?;
            let mut frame = DynamicFrame {
                idx,
                len: 0,
                memory: view,
            };

            func(&mut frame)
        }
    }

    fn output(&mut self) -> JlrsResult<Output<'frame>> {
        if self.capacity == self.len {
            return Err(AllocError::FrameOverflow(1, self.len).into());
        }

        let out = unsafe {
            let out = self.memory.new_output(self.idx, self.len);
            self.len += 1;
            out
        };

        Ok(out)
    }

    fn size(&self) -> usize {
        self.len
    }

    fn print_memory(&self) {
        self.memory.print_memory()
    }
}

impl<'frame> Frame<'frame> for DynamicFrame<'frame> {
    fn dynamic_frame<'nested, T, F: FnOnce(&mut DynamicFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        func: F,
    ) -> JlrsResult<T> {
        let mut frame = unsafe { self.nested_frame().unwrap() };
        func(&mut frame)
    }

    fn frame<'nested, T, F: FnOnce(&mut StaticFrame<'nested>) -> JlrsResult<T>>(
        &'nested mut self,
        capacity: usize,
        func: F,
    ) -> JlrsResult<T> {
        unsafe {
            let mut view = self.memory.nest_static();
            let idx = view.new_frame(capacity)?;
            let mut frame = StaticFrame {
                idx,
                capacity,
                len: 0,
                memory: view,
            };

            func(&mut frame)
        }
    }

    fn output(&mut self) -> JlrsResult<Output<'frame>> {
        unsafe {
            let out = self.memory.new_output(self.idx)?;
            self.len += 1;
            Ok(out)
        }
    }

    fn size(&self) -> usize {
        self.len
    }

    fn print_memory(&self) {
        self.memory.print_memory()
    }
}

pub(crate) mod private {
    use crate::array::Array;
    use crate::array::Dimensions;
    use crate::error::{AllocError, JlrsError, JlrsResult};
    use crate::frame::{DynamicFrame, Output, StaticFrame};
    use crate::stack::FrameIdx;
    use crate::symbol::Symbol;
    use crate::value::{Value, Values};
    use jl_sys::{
        jl_array_data, jl_array_dim, jl_array_dims, jl_array_eltype, jl_array_ndims,
        jl_array_nrows, jl_bool_type, jl_box_bool, jl_box_char, jl_box_float32, jl_box_float64,
        jl_box_int16, jl_box_int32, jl_box_int64, jl_box_int8, jl_box_uint16, jl_box_uint32,
        jl_box_uint64, jl_box_uint8, jl_char_type, jl_float32_type, jl_float64_type, jl_int16_type,
        jl_int32_type, jl_int64_type, jl_int8_type, jl_is_array, jl_is_string, jl_pchar_to_string,
        jl_string_data, jl_string_len, jl_typeis, jl_uint16_type, jl_uint32_type, jl_uint64_type,
        jl_uint8_type, jl_unbox_float32, jl_unbox_float64, jl_unbox_int16, jl_unbox_int32,
        jl_unbox_int64, jl_unbox_int8, jl_unbox_uint16, jl_unbox_uint32, jl_unbox_uint64,
        jl_unbox_uint8, jl_value_t, jl_symbol_n
    };
    use std::borrow::Cow;
    use std::mem::size_of;

    // If a trait A is used in a trait bound, the trait methods from traits that A extends become
    // available without explicitly using those base traits. By taking this struct, which can only
    // be created inside this crate, as an argument, these methods can only be called from this
    // crate.
    pub struct Internal;

    // safety: never return the symbol to the user without assigning the 'base lifetime.
    pub trait TemporarySymbol {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol>;
    }

    pub trait IntoJulia {
        // safety: Julia must have been initialized. The converted value must be protected from
        // garbage collection before calling into julia again.
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t;
    }

    pub trait JuliaType {
        // safety: Julia must have been initialized.
        unsafe fn julia_type(_: Internal) -> *mut jl_value_t;
    }

    pub trait ArrayDatatype: JuliaType {}

    pub trait TryUnbox
    where
        Self: Sized,
    {
        // safety: you can't protect anything from garbage collection inside this function, so don't
        // call Julia functions and use the results. The value should be protected from garbage
        // collection when this function is called.
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self>;
    }

    pub trait Frame<'frame> {
        // protect the value from being garbage collected while this frame is active.
        // safety: the value must be a valid Julia value
        unsafe fn protect(
            &mut self,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Result<Value<'frame, 'static>, AllocError>;

        // Create and protect multiple values from being garbage collected while this frame is active.
        fn create_many<P: IntoJulia>(
            &mut self,
            values: &[P],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError>;

        // Create and protect multiple values from being garbage collected while this frame is active.
        fn create_many_dyn(
            &mut self,
            values: &[&dyn super::IntoJulia],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError>;

        // Protect a value from being garbage collected while the output's frame is active.
        fn assign_output<'output>(
            &mut self,
            output: Output<'output>,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Value<'output, 'static>;
    }

    impl<'a> TemporarySymbol for &'a str {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol> {
            let symbol_ptr = self.as_ptr();
            let symbol = jl_symbol_n(symbol_ptr.cast(), self.len());
            Symbol::wrap(symbol)
        }
    }

    impl<'a> TemporarySymbol for Cow<'a, str> {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol> {
            let symbol_ptr = self.as_ptr().cast();
            let symbol = jl_symbol_n(symbol_ptr, self.len());
            Symbol::wrap(symbol)
        }
    }

    impl TemporarySymbol for String {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol> {
            let symbol_ptr = self.as_ptr().cast();
            let symbol = jl_symbol_n(symbol_ptr, self.len());
            Symbol::wrap(symbol)
        }
    }

    impl TemporarySymbol for &dyn AsRef<str> {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol> {
            let symbol_ptr = self.as_ref().as_ptr().cast();
            let symbol = jl_symbol_n(symbol_ptr, self.as_ref().len());
            Symbol::wrap(symbol)
        }
    }

    impl<'s> TemporarySymbol for Symbol<'s> {
        unsafe fn temporary_symbol<'symbol>(&self, _: Internal) -> Symbol<'symbol> {
            Symbol::wrap(self.ptr())
        }
    }

    macro_rules! impl_into_julia {
        ($type:ty, $boxer:ident) => {
            impl IntoJulia for $type {
                unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
                    $boxer(*self)
                }
            }
        };
        ($type:ty, $as:ty, $boxer:ident) => {
            impl IntoJulia for $type {
                unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
                    $boxer(*self as $as)
                }
            }
        };
    }

    impl_into_julia!(bool, i8, jl_box_bool);
    impl_into_julia!(char, u32, jl_box_char);
    impl_into_julia!(u8, jl_box_uint8);
    impl_into_julia!(u16, jl_box_uint16);
    impl_into_julia!(u32, jl_box_uint32);
    impl_into_julia!(u64, jl_box_uint64);
    impl_into_julia!(i8, jl_box_int8);
    impl_into_julia!(i16, jl_box_int16);
    impl_into_julia!(i32, jl_box_int32);
    impl_into_julia!(i64, jl_box_int64);
    impl_into_julia!(f32, jl_box_float32);
    impl_into_julia!(f64, jl_box_float64);

    impl IntoJulia for usize {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            if size_of::<usize>() == size_of::<u32>() {
                jl_box_uint32(*self as u32)
            } else {
                jl_box_uint64(*self as u64)
            }
        }
    }

    impl IntoJulia for isize {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            if size_of::<isize>() == size_of::<i32>() {
                jl_box_int32(*self as i32)
            } else {
                jl_box_int64(*self as i64)
            }
        }
    }

    impl<'a> IntoJulia for &'a str {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            let ptr = self.as_ptr().cast();
            let len = self.len();
            jl_pchar_to_string(ptr, len)
        }
    }

    impl<'a> IntoJulia for Cow<'a, str> {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            let ptr = self.as_ptr().cast();
            let len = self.len();
            jl_pchar_to_string(ptr, len)
        }
    }

    impl IntoJulia for String {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            let ptr = self.as_ptr().cast();
            let len = self.len();
            jl_pchar_to_string(ptr, len)
        }
    }

    impl IntoJulia for &dyn AsRef<str> {
        unsafe fn into_julia(&self, _: Internal) -> *mut jl_value_t {
            let ptr = self.as_ref().as_ptr().cast();
            let len = self.as_ref().len();
            jl_pchar_to_string(ptr, len)
        }
    }

    macro_rules! impl_julia_type {
        ($type:ty, $jl_type:expr) => {
            impl JuliaType for $type {
                unsafe fn julia_type(_: Internal) -> *mut jl_value_t {
                    $jl_type.cast()
                }
            }
        };
    }

    impl_julia_type!(u8, jl_uint8_type);
    impl_julia_type!(u16, jl_uint16_type);
    impl_julia_type!(u32, jl_uint32_type);
    impl_julia_type!(u64, jl_uint64_type);
    impl_julia_type!(i8, jl_int8_type);
    impl_julia_type!(i16, jl_int16_type);
    impl_julia_type!(i32, jl_int32_type);
    impl_julia_type!(i64, jl_int64_type);
    impl_julia_type!(f32, jl_float32_type);
    impl_julia_type!(f64, jl_float64_type);
    impl_julia_type!(bool, jl_bool_type);
    impl_julia_type!(char, jl_char_type);

    impl JuliaType for usize {
        unsafe fn julia_type(_: Internal) -> *mut jl_value_t {
            if size_of::<usize>() == size_of::<u32>() {
                jl_uint32_type.cast()
            } else {
                jl_uint64_type.cast()
            }
        }
    }

    impl JuliaType for isize {
        unsafe fn julia_type(_: Internal) -> *mut jl_value_t {
            if size_of::<isize>() == size_of::<i32>() {
                jl_int32_type.cast()
            } else {
                jl_int64_type.cast()
            }
        }
    }

    macro_rules! impl_array_datatype {
        ($type:ty) => {
            impl ArrayDatatype for $type {}
        };
    }

    impl_array_datatype!(u8);
    impl_array_datatype!(u16);
    impl_array_datatype!(u32);
    impl_array_datatype!(u64);
    impl_array_datatype!(i8);
    impl_array_datatype!(i16);
    impl_array_datatype!(i32);
    impl_array_datatype!(i64);
    impl_array_datatype!(f32);
    impl_array_datatype!(f64);
    impl_array_datatype!(usize);
    impl_array_datatype!(isize);

    macro_rules! impl_try_unbox {
        ($type:ty, $jl_type:expr, $unboxer:path) => {
            impl TryUnbox for $type {
                unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
                    if jl_typeis(value, $jl_type) {
                        return Ok($unboxer(value));
                    }

                    Err(JlrsError::WrongType.into())
                }
            }
        };
    }

    impl_try_unbox!(u8, jl_uint8_type, jl_unbox_uint8);
    impl_try_unbox!(u16, jl_uint16_type, jl_unbox_uint16);
    impl_try_unbox!(u32, jl_uint32_type, jl_unbox_uint32);
    impl_try_unbox!(u64, jl_uint64_type, jl_unbox_uint64);
    impl_try_unbox!(i8, jl_int8_type, jl_unbox_int8);
    impl_try_unbox!(i16, jl_int16_type, jl_unbox_int16);
    impl_try_unbox!(i32, jl_int32_type, jl_unbox_int32);
    impl_try_unbox!(i64, jl_int64_type, jl_unbox_int64);
    impl_try_unbox!(f32, jl_float32_type, jl_unbox_float32);
    impl_try_unbox!(f64, jl_float64_type, jl_unbox_float64);

    impl TryUnbox for bool {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
            if jl_typeis(value, jl_bool_type) {
                return Ok(jl_unbox_int8(value) != 0);
            }
            Err(JlrsError::WrongType.into())
        }
    }

    impl TryUnbox for char {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
            if jl_typeis(value, jl_char_type) {
                return std::char::from_u32(jl_unbox_uint32(value))
                    .ok_or(JlrsError::InvalidCharacter.into());
            }

            Err(JlrsError::WrongType.into())
        }
    }

    impl TryUnbox for usize {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
            if size_of::<usize>() == size_of::<u32>() {
                if jl_typeis(value, jl_uint32_type) {
                    return Ok(jl_unbox_uint32(value) as usize);
                }
            } else {
                if jl_typeis(value, jl_uint64_type) {
                    return Ok(jl_unbox_uint64(value) as usize);
                }
            }
            Err(JlrsError::WrongType.into())
        }
    }

    impl TryUnbox for isize {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
            if size_of::<isize>() == size_of::<i32>() {
                if jl_typeis(value, jl_int32_type) {
                    return Ok(jl_unbox_int32(value) as isize);
                }
            } else {
                if jl_typeis(value, jl_int64_type) {
                    return Ok(jl_unbox_int64(value) as isize);
                }
            }
            Err(JlrsError::WrongType.into())
        }
    }

    impl TryUnbox for String {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<String> {
            if !jl_is_string(value) {
                return Err(JlrsError::NotAString.into());
            }

            let len = jl_string_len(value);

            if len == 0 {
                return Ok(String::new());
            }

            // Is neither null nor dangling, we've just checked
            let raw = jl_string_data(value);
            let raw_slice = std::slice::from_raw_parts(raw, len);
            let owned_slice = Vec::from(raw_slice);
            Ok(
                String::from_utf8(owned_slice).map_err(|e| -> Box<JlrsError> {
                    let b: Box<dyn std::error::Error + Send + Sync> = Box::new(e);
                    b.into()
                })?,
            )
        }
    }

    impl<T: ArrayDatatype> TryUnbox for Array<T> {
        unsafe fn try_unbox(value: *mut jl_value_t, _: Internal) -> JlrsResult<Self> {
            if !jl_is_array(value) {
                return Err(JlrsError::NotAnArray.into());
            }
            if jl_array_eltype(value) as *mut jl_value_t != T::julia_type(Internal) {
                return Err(JlrsError::WrongType.into());
            }
            let jl_data = jl_array_data(value) as *const T;
            let n_dims = jl_array_ndims(value.cast());
            let dimensions: Dimensions = match n_dims {
                0 => return Err(JlrsError::ZeroDimension.into()),
                1 => Into::into(jl_array_nrows(value.cast()) as usize),
                2 => Into::into((
                    jl_array_dim(value.cast(), 0),
                    jl_array_dim(value.cast(), 1),
                )),
                3 => Into::into((
                    jl_array_dim(value.cast(), 0),
                    jl_array_dim(value.cast(), 1),
                    jl_array_dim(value.cast(), 2),
                )),
                ndims => Into::into(jl_array_dims(value.cast(), ndims as _)),
            };
            let sz = dimensions.size();
            let mut data = Vec::with_capacity(sz);
            let ptr = data.as_mut_ptr();
            std::ptr::copy_nonoverlapping(jl_data, ptr, sz);
            data.set_len(sz);
            Ok(Array::new(data, dimensions))
        }
    }

    impl<'frame> Frame<'frame> for StaticFrame<'frame> {
        unsafe fn protect(
            &mut self,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Result<Value<'frame, 'static>, AllocError> {
            if self.capacity == self.len {
                return Err(AllocError::FrameOverflow(1, self.len));
            }

            let out = {
                let out = self.memory.protect(self.idx, self.len, value.cast());
                self.len += 1;
                out
            };

            Ok(out)
        }

        fn create_many<P: IntoJulia>(
            &mut self,
            values: &[P],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError> {
            unsafe {
                if self.capacity < self.len + values.len() {
                    return Err(AllocError::FrameOverflow(values.len(), self.capacity()));
                }

                let offset = self.len;
                for value in values {
                    self.memory
                        .protect(self.idx, self.len, value.into_julia(Internal).cast());
                    self.len += 1;
                }

                Ok(self.memory.as_values(self.idx, offset, values.len()))
            }
        }

        fn create_many_dyn(
            &mut self,
            values: &[&dyn super::IntoJulia],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError> {
            unsafe {
                if self.capacity < self.len + values.len() {
                    return Err(AllocError::FrameOverflow(values.len(), self.capacity()));
                }

                let offset = self.len;
                for value in values {
                    self.memory
                        .protect(self.idx, self.len, value.into_julia(Internal).cast());
                    self.len += 1;
                }

                Ok(self.memory.as_values(self.idx, offset, values.len()))
            }
        }

        fn assign_output<'output>(
            &mut self,
            output: Output<'output>,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Value<'output, 'static> {
            unsafe {
                self.memory
                    .protect(FrameIdx::default(), output.offset, value.cast())
            }
        }
    }

    impl<'frame> Frame<'frame> for DynamicFrame<'frame> {
        unsafe fn protect(
            &mut self,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Result<Value<'frame, 'static>, AllocError> {
            let out = self.memory.protect(self.idx, value.cast())?;
            self.len += 1;
            Ok(out)
        }

        fn create_many<P: IntoJulia>(
            &mut self,
            values: &[P],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError> {
            unsafe {
                let offset = self.len;
                // TODO: check capacity

                for value in values {
                    match self
                        .memory
                        .protect(self.idx, value.into_julia(Internal).cast())
                    {
                        Ok(_) => (),
                        Err(AllocError::StackOverflow(_, n)) => {
                            return Err(AllocError::StackOverflow(values.len(), n))
                        }
                        _ => unreachable!(),
                    }
                    self.len += 1;
                }

                Ok(self.memory.as_values(self.idx, offset, values.len()))
            }
        }

        fn create_many_dyn(
            &mut self,
            values: &[&dyn super::IntoJulia],
            _: Internal,
        ) -> Result<Values<'frame>, AllocError> {
            unsafe {
                let offset = self.len;
                // TODO: check capacity

                for value in values {
                    self.memory
                        .protect(self.idx, value.into_julia(Internal).cast())?;
                    self.len += 1;
                }

                Ok(self.memory.as_values(self.idx, offset, values.len()))
            }
        }

        fn assign_output<'output>(
            &mut self,
            output: Output<'output>,
            value: *mut jl_value_t,
            _: Internal,
        ) -> Value<'output, 'static> {
            unsafe { self.memory.protect_output(output, value.cast()) }
        }
    }
}