ForkCloudflare (Workers AI)Cloudflare (Workers AI)published Jul 3, 2024seen 5d

cloudflare/workerd-cxx

forked from dtolnay/cxx

Open original ↗

Captured source

source ↗
published Jul 3, 2024seen 5dcaptured 15hhttp 200method plain

cloudflare/workerd-cxx

Description: workerd-cxx is a fork of cxx crate for workerd/edgeworker project

Language: Rust

License: Apache-2.0

Stars: 9

Forks: 3

Open issues: 2

Created: 2024-07-03T14:44:17Z

Pushed: 2026-06-10T15:02:50Z

Default branch: master

Fork: yes

Parent repository: dtolnay/cxx

Archived: no

README:

Workerd-cxx - C++/Rust interop for Cloudflare Workerd

This project is a fork of an excellent [cxx](https://crates.io/crates/cxx) crate for [workerd](https://github.com/cloudflare/workerd) ecosystem.

See [README.md.orig](README.md.orig) and upstream documentation for the original information about cxx crate and its basic features.

This README concerns itself with the differences between the cxx crate and the fork.

Development

Workerd-cxx can be built and consumed only using bazel. See justfile for common commands.

Differences/Features

Safety: kj::Exception integration and panic handling

Generated bridge is fully compatible with KJ exception model:

  • C++ code is assumed to always throw and returns Result.
  • Rust code returning any Result will convert errors to kj::Exception using Display.
  • Rust code using error type ::cxx::KjError can fully control information in the thrown exception.
  • kj::CanceledException causes panic with ::cxx::CanceledException payload.
  • panic with ::cxx::CanceledException causes kj::CanceledException to be thrown.
  • any other panic will result in kj::Exception to be thrown.

KJ Smart Pointers Integration

The following smart pointers can be used in ffi layers:

  • kj::Own - corresponds to kj_rs::KjOwn
  • kj::Rc - corresponds to kj_rs::KjRc
  • kj::Arc - corresponds to kj_rs::KjArc

KJ Data Structures Integration

  • kj::Maybe - corresponds to kj_rs::KjMaybe.
  • kj::Date - corresponds to kj_rs::KjDate.

KJ/Rust conversion layer

Comprehensive conversion layer is provided for many KJ types through [convert.h](kj-rs/convert.h).

Async fn support

FFI layer fully supports async fn functions. The resulting bridge will:

  • expose kj::Promise as impl Future to Rust code.
  • expose Future as kj::Promise to C++ code.

The resulting bridge promises and futures can be driven only by KJ event loop. You can still drive Rust native futures by other rust event loops like tokio when no ffi promises are used.

Rust type aliases support

We have merged in upstream PR that enables rust type aliases, which are important for reusing common cxx definitions across crates.