Skip to content

Commit

Permalink
Implement Wire for never type (#497)
Browse files Browse the repository at this point in the history
Also enable the CI test for the platform protocol schema.
  • Loading branch information
ia0 authored May 29, 2024
1 parent a754d0d commit 081c85e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/actions/ci-checks/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ inputs:
runs:
using: composite
steps:
- if: ${{ contains(fromJSON(inputs.checks), 'changelog') }}
- if: >-
${{ contains(fromJSON(inputs.checks), 'changelog')
|| contains(fromJSON(inputs.checks), 'sync') }}
uses: actions/checkout@v4
with:
fetch-depth: 0
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- main
paths:
- 'book/**'

permissions: {}

Expand Down
2 changes: 1 addition & 1 deletion crates/protocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## 0.1.0-git

<!-- Increment to skip CHANGELOG.md test: 5 -->
<!-- Increment to skip CHANGELOG.md test: 6 -->
7 changes: 2 additions & 5 deletions crates/protocol/crates/schema/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ fn check(old: &View, new: &View) -> Result<()> {
for (_, tag, api) in get_enum(&old).context("in old")? {
ensure!(old_map.insert(tag, api).is_none(), "duplicate tag {tag}");
}
for (name, tag, new_api) in get_enum(&new).context("in new")? {
for (_, tag, new_api) in get_enum(&new).context("in new")? {
if let Some(old_api) = old_map.remove(tag) {
let old_api = ViewFields(old_api);
let new_api = ViewFields(new_api);
ensure!(
old_api == new_api,
"incompatible API for {name}={tag}: {old_api} vs {new_api}"
);
ensure!(old_api == new_api, "incompatible API for {tag}: {old_api} != {new_api}");
}
}
#[cfg(feature = "full")]
Expand Down
7 changes: 3 additions & 4 deletions crates/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

#![no_std]
#![feature(doc_auto_cfg)]
#![feature(never_type)]

extern crate alloc;

use alloc::boxed::Box;
use core::convert::Infallible;

use wasefire_error::Error;
use wasefire_wire::Wire;
Expand Down Expand Up @@ -105,7 +105,7 @@ api! {
/// Errors reported by the device.
///
/// This may be returned regardless of the request type.
0 DeviceError: Infallible => Error,
0 DeviceError: ! => Error,

/// Sends a request to an applet.
1 AppletRequest: applet::Request<'a> => (),
Expand All @@ -114,8 +114,7 @@ api! {
2 AppletResponse: applet::AppletId => applet::Response<'a>,

/// Reboots the platform.
// TODO: Should return unit. Device should always respond.
3 PlatformReboot: () => Infallible,
3 PlatformReboot: () => !,

/// Starts a direct tunnel with an applet.
4 AppletTunnel: applet::Tunnel<'a> => (),
Expand Down
2 changes: 1 addition & 1 deletion crates/wire/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## 0.1.0-git

<!-- Increment to skip CHANGELOG.md test: 1 -->
<!-- Increment to skip CHANGELOG.md test: 2 -->
16 changes: 16 additions & 0 deletions crates/wire/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#![no_std]
#![feature(array_try_from_fn)]
#![feature(doc_auto_cfg)]
#![feature(never_type)]
#![feature(try_blocks)]

extern crate alloc;
Expand Down Expand Up @@ -298,6 +299,21 @@ impl<'a> internal::Wire<'a> for Error {
}
}

impl<'a> internal::Wire<'a> for ! {
#[cfg(feature = "schema")]
type Static = !;
#[cfg(feature = "schema")]
fn schema(rules: &mut Rules) {
if rules.enum_::<Self::Static>(Vec::new()) {}
}
fn encode(&self, _: &mut Writer<'a>) -> Result<(), Error> {
match *self {}
}
fn decode(_: &mut Reader<'a>) -> Result<Self, Error> {
Err(Error::user(Code::InvalidArgument))
}
}

#[internal_wire]
#[wire(crate = crate)]
enum Infallible {}
Expand Down
5 changes: 5 additions & 0 deletions scripts/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ for dir in $(find crates -name Cargo.toml -printf '%h\n' | sort); do
# add_lint $file warn rust.unused-results
done

( cd crates/protocol/crates/schema
cargo run --features=full
cargo run
)

book_example() {
local src=book/src/applet/prelude/$1.rs
local dst=examples/rust/$2/src/lib.rs
Expand Down

0 comments on commit 081c85e

Please sign in to comment.