Skip to main content

Module codec

Module codec 

Source
Expand description

Conversions between Rust values and Lean values, chosen by the Lean type.

Several Lean types share one Rust type: Array α and List α are both Vec<T>, and ByteArray and Array UInt8 are both Vec<u8>. A conversion therefore cannot be found from the Rust type alone. Generated code names it instead, as a type that implements Codec and mirrors the Lean type. Each type of this module stands for the Lean type of the same name, so Array<Option<String>> converts a Vec<Option<String>> to and from a Lean Array (Option String).

A conversion moves what it can. Codec::into_lean takes the Rust value and builds the Lean value around its buffers. Codec::from_lean takes out of the Lean value whatever nothing else refers to, as Lean’s own compiled code does when it takes a value apart. Only Codec::to_lean, which leaves the Rust value with the caller, always copies.

The conversions are safe functions because of Value: a Lean value together with its type and the one reference to it that the holder owns. The single unsafe claim at the boundary is Value::from_raw, where generated code says what a Lean function returned.

Structs§

Array
Lean’s Array α as a Vec.
BitVec
Lean’s BitVec w as the unsigned integer or the limbs W. Lean keeps a bit vector as the natural number it encodes, so a value crosses with one copy of its limbs, and one that is below 2^63 without an allocation.
Bool
Lean’s Bool.
Boxed
A value that the Rust type keeps in a Box, as recursive types must.
ByteArray
Lean’s ByteArray. The Lean object holds a Vec<u8>, so a value moves both ways.
Char
Lean’s Char.
Except
Lean’s Except ε α as a Result. Lean names the error type first, Rust the value type.
Fields
The fields of a constructor object whose reference the caller gives up. They are taken when nothing else refers to the object, and shared with it otherwise: the rule Lean’s compiled code follows when it takes a value apart.
Float
Lean’s Float.
Float32
Lean’s Float32.
FloatArray
Lean’s FloatArray. The Lean object holds a Vec<f64>, so a value moves both ways.
Generic
A value of a type that a declaration bound to Rust is generic in, as crate::Generic: the Lean object, passed on as it is. N is the type’s place among the declaration’s type parameters.
ISize
Lean’s ISize.
Int
Lean’s Int as crate::Int, which is the Lean object itself where nothing else holds the object, and a copy of it otherwise.
Int8
Lean’s Int8.
Int16
Lean’s Int16.
Int32
Lean’s Int32.
Int64
Lean’s Int64.
List
Lean’s List α as a Vec.
Nat
Lean’s Nat as crate::Nat, which is the Lean object itself where nothing else holds the object, and a copy of it otherwise.
Opaque
A type the block declares opaque. Lean cannot look inside a value of it or make one, so nothing is converted: the value is the Rust value itself, in an object. It moves into Lean and moves back out. A value the caller keeps is cloned, and so is one that Lean still holds somewhere else when it comes out, which is why the type must be Clone.
Option
Lean’s Option α.
Prod
Lean’s α × β as a pair.
String
Lean’s String. The Lean object holds a Rust String, so a value moves both ways.
UInt8
Lean’s UInt8.
UInt16
Lean’s UInt16.
UInt32
Lean’s UInt32.
UInt64
Lean’s UInt64.
USize
Lean’s USize.
Unit
Lean’s Unit, where it is part of another type.
Value
A Lean value of the type C stands for, with the one reference to it that the holder owns. Dropping it gives the reference up.
Via
The Lean type of codec C as the Rust type the primitive P chose.

Traits§

Codec
A conversion between a Rust type and the Lean type the implementor stands for.
KeptByLeanForAllThreads
A type whose values Lean may keep in a constant that every thread reads: one that is Sync.
Pass
An argument of type T for a function written in Lean: the value itself, which moves into Lean, or a reference to it. A reference to a value that Lean converts, such as a String, is copied. A reference to a value that Lean holds as it is, of an opaque or a foreign type or a collection of import Inlean, is lent for the call: Lean reads the caller’s value, and clones it only where it needs one of its own.
Primitive
A primitive type of your own: a Rust type of your choice in place of the one inlean gives a Lean type.
Repr
How Lean passes a value of a type: as an object, or as a scalar it keeps unboxed.
Scalar
A representation that is a plain number, with no reference to count.
Word
A Rust type that holds exactly the values of a BitVec w: an unsigned integer of w bits, or w / 64 limbs with the least significant first.

Functions§

given_to_a_lean_function
States that T is the type of a value that a trusted function gives to a Lean function it was passed.
returned_by_a_trusted_function
States that T is the type of a value that a trusted function returns to Lean.