pub enum HostType {
}Expand description
A type in a Host API signature, written in Cove’s source vocabulary.
This is a small enum rather than cove_syntax::ast::Type because an
ast::Type is a parsed type: every node carries a span into a source
file and its path segments are identifiers with spans of their own, all of
which would have to be invented for an operation that has no source to
point at. The rendering vocabulary is the same one — fmt::Display
produces the form the type would be written in Cove — so a signature
printed from a schema entry reads exactly like a signature written by
hand.
Add a variant when a host needs it; an unused variant is a type nobody can
produce. That used to read “the variants cover exactly the types the
shipped hosts use”, and HostType::Set and HostType::Map are why it
no longer does: no shipped host has needed either, and an embedder did
(issue #153). An embedder is
not a lesser kind of host — embedding is why HostApi is a trait — so the
list is what a host may declare rather than what this workspace happens to
ship, and crates/cove-runtime/tests/embedding.rs is where the two new
ones are produced and consumed.
Variants§
Unit
Unit, the value an operation returns when it returns nothing.
Bool
Bool.
Int
Int, a signed 64-bit integer.
String
String.
Duration
Duration, a signed count of nanoseconds.
Error
Error, the builtin error struct.
Array(&'static HostType)
Array<T>, the fixed-length immutable sequence.
Set(&'static HostType)
Set<T>, the key-ordered immutable set.
T must be one HostType::may_be_a_key allows, because a Set
element is a map key: ModuleSchema::validate is what refuses a
declaration that says otherwise, and it refuses it where the schema is
read rather than where a value is.
Map(&'static HostType, &'static HostType)
Map<K, V>, the key-ordered immutable map.
K carries the same restriction a HostType::Set element does, and
for the same reason; V carries none.
Option(&'static HostType)
Option<T>.
Result(&'static HostType, &'static HostType)
Result<T, E>. Expected failure is part of an operation’s result
type, exactly as it is in Cove source, rather than a second channel
beside it.
Named(&'static str)
A type the host declares, written qualified: http.Response.
The name is the one Cove source writes, module included, because that
is what a signature in a diagnostic has to read as. Whether it names a
TypeSchema or a ResourceSchema is the host’s business; a
signature says only which type it is.
Any
Any value at all.
This is not a missing type: it is the type of an operation whose
meaning does not depend on which value it was given. http.json
renders whatever it is handed, and a callback a host stores and calls
later is a value the host never looks inside.
What it promises, and what it costs, are different at the two ends of a signature, and both are worth stating exactly.
In a parameter it promises that every value is accepted: no argument of any type is a mistake, the compiler rejects none, and the boundary rejects none either. Nothing is given up by it, because there was never a constraint to check.
In a result it says the operation may answer with a value of any
type. That does cost something: from the call onwards the program
holds a value no schema described, so the compiler cannot prove what
a field read off it, a call made on it, or a place it is stored into
will do. Those are checked at run time and by nothing before it.
cove check reports each such call rather than letting the silence
pass for a proof — as a note, because a schema declaring Any is a
deliberate design decision and not a fault in the program that calls
it.
Implementations§
Source§impl HostType
impl HostType
Sourcepub fn may_be_a_key(&self) -> bool
pub fn may_be_a_key(&self) -> bool
Whether a value of this type may be a Map key or a Set element.
Cove’s own rule is cove_runtime::value::MapKey’s: “mutable handles
and structs containing them are not valid map keys”, because a key’s
equality must not change while a collection holds it. That rule is
about a value, and this is the most a name can say about it.
Everything made only of the scalar types qualifies, and so does any
composition of qualifying types: an Array, an Option, a Result, a
Set, or a Map is a key exactly when everything nested inside it is.
HostType::Named and HostType::Any do not, and the reason is the
same in both cases: neither says what its values are made of.
cove_runtime::schema::Admits checks a named type by the name the
value carries and deliberately looks no further — ADR 0013’s amendment
draws that line — so a schema naming reviews.PullRequest has made no
claim about the ten fields behind it, and a ResourceSchema’s handle
can never be a key at all. Any says less again. A declaration that
promised more than the boundary checks would be a promise nothing
keeps.