Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ffi/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- A type parameter whose bounds imply bytes, through a supertrait such as moq's
`IntoBytes: AsRef<[u8]>` or a `From` implementation such as moq's
`impl<B: Into<Bytes>> From<B> for Timed<Bytes>`, exports as bytes rather than
`String`. Kotlin takes a `ByteArray` there: pass text as `"text".encodeToByteArray()`.
C and C++ keep their types, and accept a payload that is not UTF-8 rather than aborting.
- `&[T]` of any type and `SocketAddrV4` and `SocketAddrV6` map, so a struct or enum whose
fields were refused only for the socket addresses is a value now, not an object, and
an item that newly exports can clash with another's C name, which takes a `rename`.
Expand Down
3 changes: 2 additions & 1 deletion ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export's, and adds nothing; another crate's type is refused.
| `impl RangeBounds<T>` of an integer | `<prefix>_bounds_<T>`: its start and end bounds |
| `Option<T>` | a null handle or buffer, or `<prefix>_option_<T>` |
| a `pub const` | a `#define` if a `bool` or integer, else a function returning it |
| `impl Into<X>`, `impl AsRef<X>` | `X` |
| `impl Into<X>`, `impl AsRef<X>`, or a bound implying one, as a supertrait does | `X`, or `&X` and then `String` or `Vec<T>` where `X` is `str` or `[T]` |
| `impl Into<X>`, where `X` does not map | what a `From` implementation into `X` takes, by its source parameter's bounds as above: `impl<B: Into<Bytes>> From<B> for Timed<Bytes>` takes `Bytes` |
| another type parameter | the first of `String`, `Bytes`, and `Vec<u8>` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance |

A generic type infers the instances it exports at, with no config: the one at its
Expand Down
2 changes: 2 additions & 0 deletions ffi/fixtures/api/examples/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ fn main() {

println!("{}", api_lib::greet("x", Shape::Square));
println!("len {}", api_lib::len(b"abc"));
println!("hex {}", api_lib::hex(Bytes::from_static(b"\xff\x00a")));
println!("timed hex {}", api_lib::timed_hex(Bytes::from_static(b"\xff\x00a")));
for event in [Event::Moved(p), Event::Named { name: "n".to_string() }, Event::Done] {
println!("{}", api_lib::describe(&event));
}
Expand Down
5 changes: 4 additions & 1 deletion ffi/fixtures/api/ffi.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package = "api-fixture"
prefix = "api"
# The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves
# out a method the others keep, `Seed` is generic, so no callback implements it, and
# out a method the others keep, `Seed` is generic, so no callback implements it, `Payload`
# consumes itself, which no callback can, `Timed` has no instance that exports, and
# `field@` leaves out a field alone where a method takes its path, as `Retry`'s and
# `Slot<u8>`'s do. `Slot<u32>` has no such method, so its path alone names its field. A
# function instance is left out by its path, as `depth_past` at `polar::Point` is.
exclude = [
"Node::next",
"Pair<u8, dep_fixture::Size>::swap",
"Payload",
"Seed",
"Slot<u32>::value",
"field@Retry::delay",
"field@Slot<u8>::value",
"Stack::depth_past<api_fixture::polar::Point>",
"Timed",
]

# The field `Retry::limit`, whose name its method takes.
Expand Down
61 changes: 61 additions & 0 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,67 @@ pub fn len(payload: &[u8]) -> usize {
payload.len()
}

/// Converts into owned bytes, as moq's `IntoBytes` does: text implements it too, but its
/// supertrait makes a parameter it bounds bytes, and owned ones, as it must be `'static`.
pub trait Payload: AsRef<[u8]> + 'static {
fn into_bytes(self) -> Bytes;
}

impl Payload for Bytes {
fn into_bytes(self) -> Bytes {
self
}
}

impl Payload for Vec<u8> {
fn into_bytes(self) -> Bytes {
self.into()
}
}

impl Payload for String {
fn into_bytes(self) -> Bytes {
self.into()
}
}

impl Payload for &'static str {
fn into_bytes(self) -> Bytes {
Bytes::from_static(self.as_bytes())
}
}

impl Payload for &'static [u8] {
fn into_bytes(self) -> Bytes {
Bytes::from_static(self)
}
}

/// A payload's bytes in hex, which need not be UTF-8.
pub fn hex(payload: impl Payload) -> String {
payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect()
}

/// A value and when it was stamped, as moq's `Timed` is, which bytes convert into.
pub struct Timed<P> {
pub value: P,
pub at: Option<Stamp>,
}

impl<B: AsRef<[u8]>> From<B> for Timed<Bytes> {
fn from(value: B) -> Self {
Self {
value: Bytes::copy_from_slice(value.as_ref()),
at: None,
}
}
}

/// A timed payload's bytes in hex, which need not be UTF-8, owned as it is `'static`.
pub fn timed_hex(payload: impl Into<Timed<Bytes>> + 'static) -> String {
hex(payload.into().value)
}

pub fn greet(name: impl Into<String>, shape: Shape) -> String {
format!("hello {}, a {shape:?}", name.into())
}
Expand Down
4 changes: 2 additions & 2 deletions ffi/moq/bench/export.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fun round(): String {
broadcast.createTrack("bench", TrackInfo.default().withTimescale(micro))
}

// The C ABI takes moq's `impl Into<Bytes>` frame as a string.
val frame = "x".repeat(100)
// A frame of 100 zero bytes, as bench/export.c writes.
val frame = ByteArray(100)
var micros = 0uL

// Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must.
Expand Down
6 changes: 3 additions & 3 deletions ffi/moq/moq.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ int main(void) {
moq_error_free(err);
CHECK(moq_timestamp_as_secs(at) == 1);

/* The last frame, then the group's end, which reads as None. */
CHECK(moq_group_producer_write_frame(writer, NULL, str("world"), &err));
/* The last frame, bytes that are not UTF-8, then the group's end, which reads as None. */
CHECK(moq_group_producer_write_frame(writer, NULL, str("\xffworld"), &err));
CHECK(moq_group_producer_finish(writer, &err));
reading = moq_group_consumer_read_frame(group);
CHECK(moq_group_consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY);
moq_group_consumer_read_frame_future_free(reading);
CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "world"));
CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "\xffworld"));
moq_frame_frame_free(frame);
reading = moq_group_consumer_read_frame(group);
frame = (moq_frame_frame *)1;
Expand Down
7 changes: 4 additions & 3 deletions ffi/moq/moq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ int main() {
CHECK(!late && is(late.error().to_string(), "frame timestamp doesn't fit its track"));
CHECK(at->as_secs() == 1);

// The last frame, then the group's end, which reads as None, both blocking.
CHECK(writer->write_frame(nullptr, "world"));
// The last frame, bytes that are not UTF-8, then the group's end, which reads as None,
// both blocking.
CHECK(writer->write_frame(nullptr, "\xffworld"));
CHECK(std::move(*writer).finish());
auto world = group.read_frame().get();
CHECK(world && world.value() && is(world.value()->payload(), "world"));
CHECK(world && world.value() && is(world.value()->payload(), "\xffworld"));
auto end = group.read_frame().get();
CHECK(end && !end.value());

Expand Down
11 changes: 6 additions & 5 deletions ffi/moq/moq.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fun main() {
scope.launch { frame = first.readFrame() }
q.drain()
expect(frame == null, "a read waits for a frame")
writer.writeFrame(null, "hello")
writer.writeFrame(null, "hello".encodeToByteArray())
expect(q.size == 1, "a frame's arrival wakes the read")
q.drain()
val hello = frame!!
Expand All @@ -89,18 +89,19 @@ fun main() {
val at = Timestamp.fromMillis(1500u)
expect(at.asSecs() == 1uL, "1500 ms is 1 s")
try {
writer.writeFrame(at, "late")
writer.writeFrame(at, "late".encodeToByteArray())
expect(false, "a timestamped frame on an untimed track is an error")
} catch (e: ErrorException) {
expect(e.message == "frame timestamp doesn't fit its track", "the late frame's error is Rust's")
}
expect(at.asSecs() == 1uL, "a copied timestamp stays usable")

// The last frame, then the group's end, which reads as null.
writer.writeFrame(null, "world")
// The last frame, bytes that are not UTF-8, then the group's end, which reads as null.
val world = byteArrayOf(0xff.toByte()) + "world".encodeToByteArray()
writer.writeFrame(null, world)
writer.finish()
runBlocking {
expect(first.readFrame()?.payload?.decodeToString() == "world", "the last frame reads")
expect(first.readFrame()?.payload?.contentEquals(world) == true, "the last frame reads")
expect(first.readFrame() == null, "a finished group reads null")
}

Expand Down
130 changes: 107 additions & 23 deletions ffi/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ use std::collections::VecDeque;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::util::elaborate;
use rustc_infer::traits::{Obligation, ObligationCause};
use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
use rustc_middle::ty::{
self, GenericArgKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingEnv,
};
use rustc_span::{Span, sym};
use rustc_span::{DUMMY_SP, Span, sym};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::ObligationCtxt;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;

use crate::ir::{
Expand Down Expand Up @@ -1849,7 +1852,8 @@ impl<'tcx> Collector<'tcx> {
}

/// The types a type parameter may be exported as, best first, which typing takes the
/// first of: `X` for `impl Into<X>` or `impl AsRef<X>`, then `String`, `Bytes`, and
/// first of: `X` for each `Into<X>` or `AsRef<X>` its bounds imply, or `&X` and then
/// `String` or `Vec<T>` where `X` is `str` or `[T]`, then `String`, `Bytes`, and
/// `Vec<u8>`, each that meets its bounds and maps.
fn choose(&self, def_id: DefId, param: Ty<'tcx>, base: &[ty::GenericArg<'tcx>]) -> Result<Vec<Ty<'tcx>>, Why> {
let tcx = self.tcx;
Expand All @@ -1862,44 +1866,66 @@ impl<'tcx> Collector<'tcx> {
.filter_map(|clause| clause.as_trait_clause())
.filter(|pred| pred.self_ty().skip_binder() == param)
.collect();
// With the bounds they imply: `IntoBytes: AsRef<[u8]>` makes a parameter it bounds bytes.
let implied: Vec<_> = elaborate(tcx, identity.clauses.iter().map(|clause| clause.skip_norm_wip())).collect();
// A parameter that must outlive the call cannot borrow what C lends for it.
let outlives = identity.clauses.iter().any(|clause| {
let outlives = implied.iter().any(|clause| {
clause
.skip_norm_wip()
.as_type_outlives_clause()
.is_some_and(|pred| pred.skip_binder().0 == param)
});
let string = tcx
.lang_items()
.string()
.map(|string| Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty()));
let vec = |elem: Ty<'tcx>| {
let vec = tcx.get_diagnostic_item(sym::Vec)?;
let alloc = tcx.lang_items().global_alloc_ty()?;
let alloc = Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty());
Some(Ty::new_adt(
tcx,
tcx.adt_def(vec),
tcx.mk_args(&[elem.into(), alloc.into()]),
))
};
let mut candidates = Vec::new();
for bound in &bounds {
let implied = (implied.iter())
.filter_map(|clause| clause.as_trait_clause())
.filter(|pred| pred.self_ty().skip_binder() == param);
for bound in implied {
let tr = bound.def_id();
let target = || bound.skip_binder().trait_ref.args.type_at(1);
if tcx.is_diagnostic_item(sym::Into, tr) || tcx.is_diagnostic_item(sym::AsRef, tr) {
let target = target();
match target.kind() {
ty::Str | ty::Slice(_) if outlives => {}
ty::Str | ty::Slice(_) => candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)),
_ => candidates.push(target),
// An `Into` target that does not map takes what converts into it instead.
let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param))
.then(|| self.sources(def_id, target()));
// A source that must outlive the call, or converts into a parameter that must, owns it.
let sources = into.into_iter().flatten().map(|(x, source)| (x, outlives || source));
for (target, outlives) in std::iter::once((target(), outlives)).chain(sources) {
let owned = match target.kind() {
ty::Str => string,
ty::Slice(elem) => vec(*elem),
_ => {
candidates.push(target);
continue;
}
};
// A borrow of what C lends, unless the parameter outlives the call, then the
// owned form, which meets bounds that only owned types implement.
if !outlives {
candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target));
}
candidates.extend(owned);
}
} else if Some(tr) == self.range_bounds {
candidates.extend(self.bounds(target()));
}
}
if let Some(string) = tcx.lang_items().string() {
candidates.push(Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty()));
}
candidates.extend(string);
if let Some(bytes) = self.bytes {
candidates.push(Ty::new_adt(tcx, tcx.adt_def(bytes), ty::List::empty()));
}
if let Some(vec) = tcx.get_diagnostic_item(sym::Vec) {
let alloc = tcx.lang_items().global_alloc_ty();
if let Some(alloc) = alloc {
let args = tcx.mk_args(&[
tcx.types.u8.into(),
Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty()).into(),
]);
candidates.push(Ty::new_adt(tcx, tcx.adt_def(vec), args));
}
}
candidates.extend(vec(tcx.types.u8));
// `impl Into<Arc<str>>` takes a `String` too, which maps where `Arc<str>` does not.
candidates
.retain(|&candidate| self.at(def_id, param, base, candidate).is_some() && self.maps(candidate, Pos::Param));
Expand All @@ -1924,6 +1950,64 @@ impl<'tcx> Collector<'tcx> {
}
}

/// What `impl Into<target>` takes through the `From` implementations into `target`
/// whose source is a type parameter: each `Into<X>` or `AsRef<X>` its bounds imply, and
/// whether they make it outlive the call, so `impl<B: Into<Bytes>> From<B> for
/// Timed<Bytes>` makes `impl Into<Timed<Bytes>>` bytes.
fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec<(Ty<'tcx>, bool)> {
let tcx = self.tcx;
let Some(from) = tcx.get_diagnostic_item(sym::From) else {
return Vec::new();
};
let Some(simple) = simplify_type(tcx, target, TreatParams::AsRigid) else {
return Vec::new();
};
let mut found = Vec::new();
for &imp in tcx
.trait_impls_of(from)
.non_blanket_impls()
.get(&simple)
.into_iter()
.flatten()
{
let (infcx, env) = tcx
.infer_ctxt()
.build_with_typing_env(TypingEnv::post_analysis(tcx, def_id));
let args = infcx.fresh_args_for_item(DUMMY_SP, imp);
let trait_ref = tcx.impl_trait_ref(imp).instantiate(tcx, args).skip_norm_wip();
let source = trait_ref.args.type_at(1);
let ocx = ObligationCtxt::new(&infcx);
if !source.is_ty_var()
|| ocx
.eq(&ObligationCause::dummy(), env, trait_ref.self_ty(), target)
.is_err()
{
continue;
}
let source = infcx.deeply_resolve_ignoring_regions(source);
let clauses = tcx.clauses_of(imp).instantiate(tcx, args).clauses;
let implied: Vec<_> = elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())).collect();
let outlives = implied.iter().any(|clause| {
clause
.as_type_outlives_clause()
.is_some_and(|pred| infcx.deeply_resolve_ignoring_regions(pred.skip_binder().0) == source)
});
for bound in (implied.iter())
.filter_map(|clause| clause.as_trait_clause())
.filter(|bound| {
tcx.is_diagnostic_item(sym::Into, bound.def_id())
|| tcx.is_diagnostic_item(sym::AsRef, bound.def_id())
}) {
let bound = infcx.deeply_resolve_ignoring_regions(bound.skip_binder());
let x = tcx.erase_and_anonymize_regions(bound.trait_ref.args.type_at(1));
if bound.self_ty() == source && !x.has_non_region_infer() && !found.contains(&(x, outlives)) {
found.push((x, outlives));
}
}
}
found
}

/// The arguments with `param` of `def_id` as `candidate`, if its bounds hold there.
fn at(
&self,
Expand Down
3 changes: 3 additions & 0 deletions ffi/tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ static int script(void) {

print("", api_greet(str("x"), API_SHAPE_SQUARE), "\n");
printf("len %zu\n", api_len(str("abc")));
api_buf binary = {.ptr = (const uint8_t *)"\xff\0a", .len = 3};
print("hex ", api_hex(binary), "\n");
print("timed hex ", api_timed_hex(binary), "\n");
api_event events[3] = {
{.tag = API_EVENT_MOVED, .moved = {._0 = p}},
{.tag = API_EVENT_NAMED, .named = {.name = str("n")}},
Expand Down
3 changes: 3 additions & 0 deletions ffi/tests/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ int script() {

print("", api::greet("x", api::shape::square), "\n");
std::printf("len %zu\n", api::len("abc"));
const std::uint8_t binary[] = {0xff, 0x00, 'a'};
print("hex ", api::hex(api::view(binary, sizeof binary)), "\n");
print("timed hex ", api::timed_hex(api::view(binary, sizeof binary)), "\n");
api::event events[] = {api::event_moved{p}, api::event_named{api::buf("n")}, api::event_done{}};
for (const auto &event : events) {
print("", api::describe(event), "\n");
Expand Down
Loading