1use crate::object::{self as o, box_, dec, dec_ref, inc, Obj};
20use std::marker::PhantomData;
21
22pub trait Repr: Copy {
24 fn into_object(self) -> Obj;
27
28 unsafe fn take(object: Obj) -> Self;
34
35 unsafe fn share(object: Obj) -> Self;
41
42 unsafe fn release(self);
47
48 unsafe fn retain(self) -> Self;
53}
54
55impl Repr for Obj {
56 #[inline(always)]
57 fn into_object(self) -> Obj {
58 self
59 }
60
61 #[inline(always)]
62 unsafe fn take(object: Obj) -> Obj {
63 object
64 }
65
66 #[inline(always)]
67 unsafe fn share(object: Obj) -> Obj {
68 inc(object);
69 object
70 }
71
72 #[inline(always)]
73 unsafe fn release(self) {
74 dec(self)
75 }
76
77 #[inline(always)]
78 unsafe fn retain(self) -> Obj {
79 inc(self);
80 self
81 }
82}
83
84pub trait Scalar: Repr {}
86
87macro_rules! scalar_reprs {
88 ($($ty:ty: |$value:ident| $boxed:expr, |$object:ident| $unboxed:expr;)*) => {$(
89 impl Repr for $ty {
90 #[inline(always)]
91 fn into_object(self) -> Obj {
92 let $value = self;
93 #[allow(unused_unsafe)]
95 unsafe {
96 $boxed
97 }
98 }
99
100 #[inline(always)]
101 unsafe fn take($object: Obj) -> $ty {
102 let value = $unboxed;
103 dec($object);
104 value
105 }
106
107 #[inline(always)]
108 unsafe fn share($object: Obj) -> $ty {
109 $unboxed
110 }
111
112 #[inline(always)]
113 unsafe fn release(self) {}
114
115 #[inline(always)]
116 unsafe fn retain(self) -> $ty {
117 self
118 }
119 }
120
121 impl Scalar for $ty {}
122 )*};
123}
124
125scalar_reprs! {
126 u8: |v| box_(v as usize), |x| o::unbox(x) as u8;
127 u16: |v| box_(v as usize), |x| o::unbox(x) as u16;
128 u32: |v| o::box_uint32(v), |x| o::unbox_uint32(x);
129 u64: |v| o::box_uint64(v), |x| o::unbox_uint64(x);
130 usize: |v| o::box_usize(v), |x| o::unbox_usize(x);
131 f64: |v| o::box_float(v), |x| o::unbox_float(x);
132 f32: |v| o::box_float32(v), |x| o::unbox_float32(x);
133}
134
135#[repr(transparent)]
142pub struct Value<C: Codec>(C::Repr, PhantomData<C>);
143
144impl<C: Codec> Value<C> {
145 #[inline(always)]
149 pub unsafe fn from_raw(repr: C::Repr) -> Value<C> {
150 Value(repr, PhantomData)
151 }
152
153 #[inline(always)]
155 pub fn from_scalar(repr: C::Repr) -> Value<C>
156 where
157 C::Repr: Scalar,
158 {
159 Value(repr, PhantomData)
160 }
161
162 #[inline(always)]
164 pub fn into_raw(self) -> C::Repr {
165 let repr = self.0;
166 std::mem::forget(self);
167 repr
168 }
169
170 #[inline(always)]
172 pub fn as_raw(&self) -> C::Repr {
173 self.0
174 }
175
176 #[inline(always)]
178 pub fn into_object(self) -> Obj {
179 self.into_raw().into_object()
180 }
181
182 #[inline(always)]
188 pub unsafe fn take(object: Obj) -> Value<C> {
189 Value(C::Repr::take(object), PhantomData)
190 }
191
192 #[inline(always)]
198 pub unsafe fn share(object: Obj) -> Value<C> {
199 Value(C::Repr::share(object), PhantomData)
200 }
201
202 #[inline(always)]
204 pub fn retain(&self) -> Value<C> {
205 Value(unsafe { self.0.retain() }, PhantomData)
206 }
207
208 #[inline(always)]
213 pub unsafe fn cast<D: Codec<Repr = C::Repr>>(self) -> Value<D> {
214 Value(self.into_raw(), PhantomData)
215 }
216}
217
218impl<C: Codec> Drop for Value<C> {
219 #[inline(always)]
220 fn drop(&mut self) {
221 unsafe { self.0.release() }
222 }
223}
224
225pub trait Codec: Sized {
227 type Rust;
229
230 type Repr: Repr;
232
233 fn into_lean(value: Self::Rust) -> Value<Self>;
235
236 fn to_lean(value: &Self::Rust) -> Value<Self>;
238
239 fn from_lean(value: Value<Self>) -> Self::Rust;
245
246 #[doc(hidden)]
255 unsafe fn lent(value: &Self::Rust) -> Value<Self> {
256 Self::to_lean(value)
257 }
258
259 fn lend<R>(value: &Value<Self>, f: impl FnOnce(&Self::Rust) -> R) -> R {
266 let copy = Self::from_lean(value.retain());
267 f(©)
268 }
269}
270
271pub trait KeptByLeanForAllThreads {}
277
278impl<T: Sync> KeptByLeanForAllThreads for T {}
279
280#[inline(always)]
286pub fn returned_by_a_trusted_function<T: KeptByLeanForAllThreads>() {}
287
288#[inline(always)]
295pub fn given_to_a_lean_function<T: KeptByLeanForAllThreads>() {}
296
297macro_rules! scalars {
298 ($($name:ident: $rust:ty as $repr:ty, |$value:ident| $to:expr, |$raw:ident| $from:expr;)*) => {$(
299 #[doc = concat!("Lean's `", stringify!($name), "`.")]
300 pub struct $name;
301
302 impl Codec for $name {
303 type Rust = $rust;
304 type Repr = $repr;
305
306 #[inline(always)]
307 fn into_lean($value: $rust) -> Value<$name> {
308 Value::from_scalar($to)
309 }
310
311 #[inline(always)]
312 fn to_lean(value: &$rust) -> Value<$name> {
313 Self::into_lean(*value)
314 }
315
316 #[inline(always)]
317 fn from_lean(value: Value<$name>) -> $rust {
318 let $raw = value.into_raw();
319 $from
320 }
321 }
322 )*};
323}
324
325scalars! {
328 UInt8: u8 as u8, |v| v, |r| r;
329 UInt16: u16 as u16, |v| v, |r| r;
330 UInt32: u32 as u32, |v| v, |r| r;
331 UInt64: u64 as u64, |v| v, |r| r;
332 USize: usize as usize, |v| v, |r| r;
333 Int8: i8 as u8, |v| v as u8, |r| r as i8;
334 Int16: i16 as u16, |v| v as u16, |r| r as i16;
335 Int32: i32 as u32, |v| v as u32, |r| r as i32;
336 Int64: i64 as u64, |v| v as u64, |r| r as i64;
337 ISize: isize as usize, |v| v as usize, |r| r as isize;
338 Bool: bool as u8, |v| v as u8, |r| r != 0;
339 Char: char as u32, |v| v as u32, |r| crate::char_of_u32(r);
340 Float: f64 as f64, |v| v, |r| r;
341 Float32: f32 as f32, |v| v, |r| r;
342}
343
344pub struct Generic<'a, const N: usize>(PhantomData<&'a ()>);
348
349impl<'a, const N: usize> Codec for Generic<'a, N> {
350 type Rust = crate::Generic<'a, N>;
351 type Repr = Obj;
352
353 #[inline]
354 fn into_lean(value: crate::Generic<'a, N>) -> Value<Self> {
355 unsafe { Value::from_raw(value.into_raw()) }
356 }
357
358 #[inline]
359 fn to_lean(value: &crate::Generic<'a, N>) -> Value<Self> {
360 Self::into_lean(value.clone())
361 }
362
363 #[inline]
364 fn from_lean(value: Value<Self>) -> crate::Generic<'a, N> {
365 unsafe { crate::Generic::from_raw(value.into_raw()) }
366 }
367}
368
369pub struct Unit;
371
372impl Codec for Unit {
373 type Rust = ();
374 type Repr = Obj;
375
376 fn into_lean(_value: ()) -> Value<Unit> {
377 unsafe { Value::from_raw(box_(0)) }
378 }
379
380 fn to_lean(_value: &()) -> Value<Unit> {
381 unsafe { Value::from_raw(box_(0)) }
382 }
383
384 fn from_lean(_value: Value<Unit>) {}
385}
386
387pub struct Nat;
390
391impl Codec for Nat {
392 type Rust = crate::Nat;
393 type Repr = Obj;
394
395 #[inline]
396 fn into_lean(value: crate::Nat) -> Value<Nat> {
397 unsafe { Value::from_raw(value.into_raw()) }
398 }
399
400 #[inline]
401 fn to_lean(value: &crate::Nat) -> Value<Nat> {
402 Self::into_lean(value.clone())
403 }
404
405 #[inline]
406 fn from_lean(value: Value<Nat>) -> crate::Nat {
407 unsafe { crate::Nat::owning(value.into_raw()) }
408 }
409
410 #[inline]
411 fn lend<R>(value: &Value<Nat>, f: impl FnOnce(&crate::Nat) -> R) -> R {
412 let nat = std::mem::ManuallyDrop::new(unsafe { crate::Nat::from_raw(value.as_raw()) });
414 f(&nat)
415 }
416}
417
418pub struct Int;
421
422impl Codec for Int {
423 type Rust = crate::Int;
424 type Repr = Obj;
425
426 #[inline]
427 fn into_lean(value: crate::Int) -> Value<Int> {
428 unsafe { Value::from_raw(value.into_raw()) }
429 }
430
431 #[inline]
432 fn to_lean(value: &crate::Int) -> Value<Int> {
433 Self::into_lean(value.clone())
434 }
435
436 #[inline]
437 fn from_lean(value: Value<Int>) -> crate::Int {
438 unsafe { crate::Int::owning(value.into_raw()) }
439 }
440
441 #[inline]
442 fn lend<R>(value: &Value<Int>, f: impl FnOnce(&crate::Int) -> R) -> R {
443 let int = std::mem::ManuallyDrop::new(unsafe { crate::Int::from_raw(value.as_raw()) });
444 f(&int)
445 }
446}
447
448pub struct String;
450
451impl Codec for String {
452 type Rust = std::string::String;
453 type Repr = Obj;
454
455 fn into_lean(value: std::string::String) -> Value<String> {
456 unsafe { Value::from_raw(crate::string::from_string(value)) }
457 }
458
459 fn to_lean(value: &std::string::String) -> Value<String> {
460 unsafe { Value::from_raw(crate::string::mk_string(value)) }
461 }
462
463 fn from_lean(value: Value<String>) -> std::string::String {
464 unsafe { crate::string::into_string(value.into_raw()) }
465 }
466
467 fn lend<R>(value: &Value<String>, f: impl FnOnce(&std::string::String) -> R) -> R {
468 f(unsafe { crate::string::held_string(value.as_raw()) })
469 }
470}
471
472pub struct ByteArray;
474
475impl Codec for ByteArray {
476 type Rust = Vec<u8>;
477 type Repr = Obj;
478
479 fn into_lean(value: Vec<u8>) -> Value<ByteArray> {
480 unsafe { Value::from_raw(crate::sarray::byte_array_from_vec(value)) }
481 }
482
483 fn to_lean(value: &Vec<u8>) -> Value<ByteArray> {
484 Self::into_lean(value.clone())
485 }
486
487 fn from_lean(value: Value<ByteArray>) -> Vec<u8> {
488 unsafe { crate::sarray::byte_array_into_vec(value.into_raw()) }
489 }
490
491 fn lend<R>(value: &Value<ByteArray>, f: impl FnOnce(&Vec<u8>) -> R) -> R {
492 f(unsafe { crate::sarray::held_bytes(value.as_raw()) })
493 }
494}
495
496pub struct FloatArray;
498
499impl Codec for FloatArray {
500 type Rust = Vec<f64>;
501 type Repr = Obj;
502
503 fn into_lean(value: Vec<f64>) -> Value<FloatArray> {
504 unsafe { Value::from_raw(crate::sarray::float_array_from_vec(value)) }
505 }
506
507 fn to_lean(value: &Vec<f64>) -> Value<FloatArray> {
508 Self::into_lean(value.clone())
509 }
510
511 fn from_lean(value: Value<FloatArray>) -> Vec<f64> {
512 unsafe { crate::sarray::float_array_into_vec(value.into_raw()) }
513 }
514
515 fn lend<R>(value: &Value<FloatArray>, f: impl FnOnce(&Vec<f64>) -> R) -> R {
516 f(unsafe { crate::sarray::held_floats(value.as_raw()) })
517 }
518}
519
520struct Owned(Vec<Obj>);
524
525impl Owned {
526 fn into_vec(mut self) -> Vec<Obj> {
527 std::mem::take(&mut self.0)
528 }
529}
530
531impl Drop for Owned {
532 fn drop(&mut self) {
533 for &object in &self.0 {
534 unsafe { dec(object) }
535 }
536 }
537}
538
539struct Rest(std::vec::IntoIter<Obj>);
541
542impl Drop for Rest {
543 fn drop(&mut self) {
544 for object in &mut self.0 {
545 unsafe { dec(object) }
546 }
547 }
548}
549
550pub struct Fields {
554 object: Obj,
555 exclusive: bool,
556}
557
558impl Fields {
559 #[inline(always)]
563 pub unsafe fn of(object: Obj) -> Fields {
564 Fields { object, exclusive: o::is_exclusive(object) }
565 }
566
567 #[inline(always)]
572 pub unsafe fn object<C: Codec>(&self, i: u32) -> Value<C> {
573 let field = o::ctor_get(self.object, i);
574 if self.exclusive {
575 Value::take(field)
576 } else {
577 Value::share(field)
578 }
579 }
580
581 #[inline(always)]
586 pub unsafe fn done(self) {
587 if self.exclusive {
588 o::free_object(self.object)
589 } else {
590 dec_ref(self.object)
591 }
592 }
593}
594
595pub struct Array<C>(PhantomData<C>);
597
598impl<C: Codec> Array<C> {
599 pub fn from_values(items: impl ExactSizeIterator<Item = Value<C>>) -> Value<Self> {
601 let mut objects = Owned(Vec::with_capacity(items.len()));
602 for item in items {
603 objects.0.push(item.into_object());
604 }
605 unsafe { Value::from_raw(crate::array::from_vec(objects.into_vec())) }
606 }
607}
608
609impl<C: Codec> Codec for Array<C> {
610 type Rust = Vec<C::Rust>;
611 type Repr = Obj;
612
613 fn into_lean(value: Vec<C::Rust>) -> Value<Self> {
614 Self::from_values(value.into_iter().map(C::into_lean))
615 }
616
617 fn to_lean(value: &Vec<C::Rust>) -> Value<Self> {
618 Self::from_values(value.iter().map(C::to_lean))
619 }
620
621 fn from_lean(value: Value<Self>) -> Vec<C::Rust> {
622 let object = value.into_raw();
623 unsafe {
624 if o::is_exclusive(object) {
625 let mut rest = Rest(crate::array::into_vec(object).into_iter());
627 let mut items = Vec::with_capacity(rest.0.len());
628 while let Some(item) = rest.0.next() {
629 items.push(C::from_lean(Value::take(item)));
630 }
631 items
632 } else {
633 let _shared = Value::<Self>::from_raw(object);
636 crate::array::elements(object).iter().map(|&item| C::from_lean(Value::share(item))).collect()
637 }
638 }
639 }
640}
641
642pub struct List<C>(PhantomData<C>);
644
645impl<C: Codec> List<C> {
646 fn build(items: impl Iterator<Item = Obj>) -> Value<Self> {
648 let mut list: Value<Self> = unsafe { Value::from_raw(box_(0)) };
651 for item in items {
652 unsafe {
653 let cell = o::alloc_ctor(1, 2, 0, 0);
655 o::ctor_set(cell, 0, item);
656 o::ctor_set(cell, 1, list.into_raw());
657 list = Value::from_raw(cell);
658 }
659 }
660 list
661 }
662}
663
664impl<C: Codec> Codec for List<C> {
665 type Rust = Vec<C::Rust>;
666 type Repr = Obj;
667
668 fn into_lean(value: Vec<C::Rust>) -> Value<Self> {
669 Self::build(value.into_iter().rev().map(|item| C::into_lean(item).into_object()))
670 }
671
672 fn to_lean(value: &Vec<C::Rust>) -> Value<Self> {
673 Self::build(value.iter().rev().map(|item| C::to_lean(item).into_object()))
674 }
675
676 fn from_lean(value: Value<Self>) -> Vec<C::Rust> {
677 let mut items = Vec::new();
678 let mut rest = value;
679 loop {
680 let cell = rest.into_raw();
681 if o::is_scalar(cell) {
682 return items;
683 }
684 unsafe {
685 let fields = Fields::of(cell);
686 let head = fields.object::<C>(0);
687 let tail = fields.object::<Self>(1);
688 fields.done();
689 rest = tail;
690 items.push(C::from_lean(head));
691 }
692 }
693 }
694}
695
696pub struct Option<C>(PhantomData<C>);
698
699impl<C: Codec> Option<C> {
700 fn some(field: Value<C>) -> Value<Self> {
701 unsafe {
702 let some = o::alloc_ctor(1, 1, 0, 0);
703 o::ctor_set(some, 0, field.into_object());
704 Value::from_raw(some)
705 }
706 }
707}
708
709impl<C: Codec> Codec for Option<C> {
710 type Rust = core::option::Option<C::Rust>;
711 type Repr = Obj;
712
713 fn into_lean(value: Self::Rust) -> Value<Self> {
714 match value {
715 None => unsafe { Value::from_raw(box_(0)) },
716 Some(value) => Self::some(C::into_lean(value)),
717 }
718 }
719
720 fn to_lean(value: &Self::Rust) -> Value<Self> {
721 match value {
722 None => unsafe { Value::from_raw(box_(0)) },
723 Some(value) => Self::some(C::to_lean(value)),
724 }
725 }
726
727 fn from_lean(value: Value<Self>) -> Self::Rust {
728 let object = value.into_raw();
729 if o::is_scalar(object) {
730 return None;
731 }
732 unsafe {
733 let fields = Fields::of(object);
734 let field = fields.object::<C>(0);
735 fields.done();
736 Some(C::from_lean(field))
737 }
738 }
739}
740
741pub struct Prod<A, B>(PhantomData<(A, B)>);
743
744impl<A: Codec, B: Codec> Prod<A, B> {
745 fn pair(first: Value<A>, second: Value<B>) -> Value<Self> {
746 unsafe {
747 let pair = o::alloc_ctor(0, 2, 0, 0);
748 o::ctor_set(pair, 0, first.into_object());
749 o::ctor_set(pair, 1, second.into_object());
750 Value::from_raw(pair)
751 }
752 }
753
754 pub fn from_refs(first: &A::Rust, second: &B::Rust) -> Value<Self> {
756 Self::pair(A::to_lean(first), B::to_lean(second))
757 }
758}
759
760impl<A: Codec, B: Codec> Codec for Prod<A, B> {
761 type Rust = (A::Rust, B::Rust);
762 type Repr = Obj;
763
764 fn into_lean(value: Self::Rust) -> Value<Self> {
765 let first = A::into_lean(value.0);
766 Self::pair(first, B::into_lean(value.1))
767 }
768
769 fn to_lean(value: &Self::Rust) -> Value<Self> {
770 Self::from_refs(&value.0, &value.1)
771 }
772
773 fn from_lean(value: Value<Self>) -> Self::Rust {
774 unsafe {
775 let fields = Fields::of(value.into_raw());
776 let (first, second) = (fields.object::<A>(0), fields.object::<B>(1));
777 fields.done();
778 let first = A::from_lean(first);
779 (first, B::from_lean(second))
780 }
781 }
782}
783
784pub struct Except<E, A>(PhantomData<(E, A)>);
786
787impl<E: Codec, A: Codec> Except<E, A> {
788 fn with(tag: u32, field: Obj) -> Value<Self> {
790 unsafe {
791 let object = o::alloc_ctor(tag, 1, 0, 0);
792 o::ctor_set(object, 0, field);
793 Value::from_raw(object)
794 }
795 }
796}
797
798impl<E: Codec, A: Codec> Codec for Except<E, A> {
799 type Rust = Result<A::Rust, E::Rust>;
800 type Repr = Obj;
801
802 fn into_lean(value: Self::Rust) -> Value<Self> {
803 match value {
804 Err(error) => Self::with(0, E::into_lean(error).into_object()),
805 Ok(value) => Self::with(1, A::into_lean(value).into_object()),
806 }
807 }
808
809 fn to_lean(value: &Self::Rust) -> Value<Self> {
810 match value {
811 Err(error) => Self::with(0, E::to_lean(error).into_object()),
812 Ok(value) => Self::with(1, A::to_lean(value).into_object()),
813 }
814 }
815
816 fn from_lean(value: Value<Self>) -> Self::Rust {
817 let object = value.into_raw();
818 unsafe {
819 let ok = o::obj_tag(object) == 1;
820 let fields = Fields::of(object);
821 if ok {
822 let field = fields.object::<A>(0);
823 fields.done();
824 Ok(A::from_lean(field))
825 } else {
826 let field = fields.object::<E>(0);
827 fields.done();
828 Err(E::from_lean(field))
829 }
830 }
831 }
832}
833
834pub struct Boxed<C>(PhantomData<C>);
836
837impl<C: Codec> Codec for Boxed<C> {
838 type Rust = Box<C::Rust>;
839 type Repr = C::Repr;
840
841 fn into_lean(value: Self::Rust) -> Value<Self> {
842 unsafe { C::into_lean(*value).cast() }
843 }
844
845 fn to_lean(value: &Self::Rust) -> Value<Self> {
846 unsafe { C::to_lean(value).cast() }
847 }
848
849 fn from_lean(value: Value<Self>) -> Self::Rust {
850 Box::new(C::from_lean(unsafe { value.cast() }))
851 }
852}
853
854pub struct Opaque<T>(PhantomData<T>);
859
860impl<T: Clone + 'static> Codec for Opaque<T> {
861 type Rust = T;
862 type Repr = Obj;
863
864 fn into_lean(value: T) -> Value<Self> {
865 unsafe { Value::from_raw(o::hold_external(value)) }
866 }
867
868 fn to_lean(value: &T) -> Value<Self> {
869 Self::into_lean(value.clone())
870 }
871
872 unsafe fn lent(value: &T) -> Value<Self> {
873 Value::from_raw(o::lend_external(value))
874 }
875
876 fn from_lean(value: Value<Self>) -> T {
877 unsafe {
878 if o::is_exclusive(value.as_raw()) && !o::is_lent(value.as_raw()) {
880 o::take_external(value.into_raw())
881 } else {
882 o::external::<T>(value.as_raw()).clone()
885 }
886 }
887 }
888
889 fn lend<R>(value: &Value<Self>, f: impl FnOnce(&T) -> R) -> R {
890 f(unsafe { o::external::<T>(value.as_raw()) })
891 }
892}
893
894pub(crate) fn fixed_limbs<const N: usize>(limbs: &[u64]) -> core::option::Option<[u64; N]> {
896 let mut out = [0u64; N];
897 out.get_mut(..limbs.len())?.copy_from_slice(limbs);
898 Some(out)
899}
900
901pub trait Word: Copy {
904 #[doc(hidden)]
905 fn to_nat(self) -> Obj;
906
907 #[doc(hidden)]
910 unsafe fn of_nat(nat: Obj) -> Self;
911}
912
913macro_rules! words {
914 ($($ty:ty),*) => {$(
915 impl Word for $ty {
916 fn to_nat(self) -> Obj {
917 crate::nat::uint64_to_nat(self as u64)
918 }
919
920 unsafe fn of_nat(nat: Obj) -> $ty {
921 crate::nat::with_limbs(nat, |limbs| limbs.first().copied().unwrap_or(0)) as $ty
922 }
923 }
924 )*};
925}
926
927words!(u8, u16, u32, u64);
928
929impl Word for u128 {
930 fn to_nat(self) -> Obj {
931 crate::nat::of_u128(self)
932 }
933
934 unsafe fn of_nat(nat: Obj) -> u128 {
935 let [low, high] = <[u64; 2]>::of_nat(nat);
936 (high as u128) << 64 | low as u128
937 }
938}
939
940impl<const N: usize> Word for [u64; N] {
941 fn to_nat(self) -> Obj {
942 crate::nat::of_limbs(&self)
943 }
944
945 unsafe fn of_nat(nat: Obj) -> [u64; N] {
946 match crate::nat::with_limbs(nat, fixed_limbs) {
947 Some(limbs) => limbs,
948 None => panic!("inlean: Lean returned a BitVec wider than its type"),
949 }
950 }
951}
952
953pub struct BitVec<W>(PhantomData<W>);
957
958impl<W: Word> Codec for BitVec<W> {
959 type Rust = W;
960 type Repr = Obj;
961
962 fn into_lean(value: W) -> Value<Self> {
963 unsafe { Value::from_raw(value.to_nat()) }
964 }
965
966 fn to_lean(value: &W) -> Value<Self> {
967 Self::into_lean(*value)
968 }
969
970 fn from_lean(value: Value<Self>) -> W {
971 unsafe { W::of_nat(value.as_raw()) }
973 }
974}
975
976pub trait Primitive {
1012 type Lean;
1014 type Rust;
1016
1017 fn to_lean(value: &Self::Rust) -> Self::Lean;
1019
1020 fn into_lean(value: Self::Rust) -> Self::Lean {
1023 Self::to_lean(&value)
1024 }
1025
1026 fn from_lean(value: Self::Lean) -> Self::Rust;
1027}
1028
1029pub struct Via<C, P>(PhantomData<(C, P)>);
1031
1032impl<C: Codec, P: Primitive<Lean = C::Rust>> Codec for Via<C, P> {
1033 type Rust = P::Rust;
1034 type Repr = C::Repr;
1035
1036 fn into_lean(value: P::Rust) -> Value<Self> {
1037 unsafe { C::into_lean(P::into_lean(value)).cast() }
1038 }
1039
1040 fn to_lean(value: &P::Rust) -> Value<Self> {
1041 unsafe { C::into_lean(P::to_lean(value)).cast() }
1043 }
1044
1045 fn from_lean(value: Value<Self>) -> P::Rust {
1046 P::from_lean(C::from_lean(unsafe { value.cast() }))
1047 }
1048}
1049
1050pub trait Pass<T>: Sized {
1068 #[doc(hidden)]
1070 fn pass<C: Codec<Rust = T>>(self) -> Value<C>;
1071
1072 #[doc(hidden)]
1080 #[inline]
1081 unsafe fn lend<C: Codec<Rust = T>>(self) -> Value<C> {
1082 self.pass()
1083 }
1084}
1085
1086impl<T> Pass<T> for T {
1087 #[inline]
1088 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1089 C::into_lean(self)
1090 }
1091}
1092
1093impl<T> Pass<T> for &T {
1094 #[inline]
1095 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1096 C::to_lean(self)
1097 }
1098
1099 #[inline]
1100 unsafe fn lend<C: Codec<Rust = T>>(self) -> Value<C> {
1101 C::lent(self)
1102 }
1103}
1104
1105impl Pass<std::string::String> for &str {
1106 #[inline]
1107 fn pass<C: Codec<Rust = std::string::String>>(self) -> Value<C> {
1108 C::into_lean(self.to_owned())
1109 }
1110}
1111
1112impl<T: Clone> Pass<Vec<T>> for &[T] {
1113 #[inline]
1114 fn pass<C: Codec<Rust = Vec<T>>>(self) -> Value<C> {
1115 C::into_lean(self.to_vec())
1116 }
1117}
1118
1119impl<T: Clone, const N: usize> Pass<Vec<T>> for &[T; N] {
1120 #[inline]
1121 fn pass<C: Codec<Rust = Vec<T>>>(self) -> Value<C> {
1122 C::into_lean(self.to_vec())
1123 }
1124}
1125
1126impl<T, const N: usize> Pass<Vec<T>> for [T; N] {
1127 #[inline]
1128 fn pass<C: Codec<Rust = Vec<T>>>(self) -> Value<C> {
1129 C::into_lean(Vec::from(self))
1130 }
1131}
1132
1133impl<T> Pass<T> for Box<T> {
1134 #[inline]
1135 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1136 C::into_lean(*self)
1137 }
1138}
1139
1140impl<T> Pass<T> for &Box<T> {
1141 #[inline]
1142 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1143 C::to_lean(self)
1144 }
1145
1146 #[inline]
1147 unsafe fn lend<C: Codec<Rust = T>>(self) -> Value<C> {
1148 C::lent(self)
1149 }
1150}
1151
1152impl<T> Pass<T> for &std::rc::Rc<T> {
1153 #[inline]
1154 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1155 C::to_lean(self)
1156 }
1157
1158 #[inline]
1159 unsafe fn lend<C: Codec<Rust = T>>(self) -> Value<C> {
1160 C::lent(self)
1161 }
1162}
1163
1164impl<T> Pass<T> for &std::sync::Arc<T> {
1165 #[inline]
1166 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1167 C::to_lean(self)
1168 }
1169
1170 #[inline]
1171 unsafe fn lend<C: Codec<Rust = T>>(self) -> Value<C> {
1172 C::lent(self)
1173 }
1174}
1175
1176impl<T: Clone> Pass<T> for std::borrow::Cow<'_, T> {
1177 #[inline]
1178 fn pass<C: Codec<Rust = T>>(self) -> Value<C> {
1179 C::into_lean(self.into_owned())
1180 }
1181}
1182
1183impl Pass<std::string::String> for std::borrow::Cow<'_, str> {
1184 #[inline]
1185 fn pass<C: Codec<Rust = std::string::String>>(self) -> Value<C> {
1186 C::into_lean(self.into_owned())
1187 }
1188}
1189
1190impl<T: Clone> Pass<Vec<T>> for std::borrow::Cow<'_, [T]> {
1191 #[inline]
1192 fn pass<C: Codec<Rust = Vec<T>>>(self) -> Value<C> {
1193 C::into_lean(self.into_owned())
1194 }
1195}