Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
4d83c65
chore(quest): claim quest/m1/ffi-export/iterators.md
kixelated Oct 29, 2026
c943c81
feat(ffi): return another crate's iterator as the array of its items
kixelated Oct 29, 2026
858c9b0
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export/i…
kixelated Oct 29, 2026
59aa0cf
fix(ffi): collect only iterators no caller could tell apart from lazy…
kixelated Oct 29, 2026
3baff99
fix(ffi): check the items and iterators an iterator's adapters drive
kixelated Oct 29, 2026
0a37657
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export/i…
kixelated Oct 29, 2026
be31c15
fix(ffi): refuse iterators whose adapters may drop another crate's it…
kixelated Oct 29, 2026
70ec74f
fix(ffi): collect only listed iterators over existing storage
kixelated Oct 29, 2026
30045ba
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export/i…
kixelated Oct 29, 2026
5be40c8
fix(ffi): turn only listed collections into iterators
kixelated Oct 29, 2026
0589b58
fix(ffi): keep another crate's types to the items a collected iterato…
kixelated Oct 29, 2026
9109b03
fix(ffi): allow another crate's types only in the items a collected i…
kixelated Oct 29, 2026
9238258
fix(ffi): refuse another crate's allocator or pattern wherever it sits
kixelated Oct 29, 2026
55de773
fix(ffi): resolve where a collected iterator's allocator or pattern i…
kixelated Oct 29, 2026
6d5df16
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export/i…
kixelated Oct 29, 2026
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
3 changes: 2 additions & 1 deletion ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exclude = ["track::Producer::poll_closed"]
# Another package exported into the same library, which the package builds with: its
# own `exclude` and `rename`, by paths from its crate root.
[crates.moq-pattern]
exclude = ["Patterns::iter"]
exclude = ["Pattern::new"]
```

## Rules
Expand Down Expand Up @@ -78,6 +78,7 @@ export's, and adds nothing; another crate's type is refused.
| `url::Url` | an object `<prefix>_url`, made by `<prefix>_url_parse`, which fails with the parse error's message, and printed by `_to_string` |
| `serde_json::Value` | an object `<prefix>_json`, made by `<prefix>_json_parse` from JSON text, which fails with serde_json's message, and read back by `_to_text`; its contents are the caller's JSON library's to read |
| `Vec<T>` and `&[T]` of any other `T` | `<prefix>_vec_<T>`: an array of `T`'s C values, viewed passed in and owned returned, a slice as a clone; of references only returned |
| a returned iterator, such as `slice::Iter` or an `impl Iterator`, that no other rule maps, outside a future, made of core's and alloc's iterators over a slice, string, `Option`, `Result`, or collection, through `rev`, `chain`, `flatten`, `fuse`, `enumerate`, `peekable`, `copied`, or `cloned`, with no closure, function, or trait object inside | what `collect::<Vec<_>>()` makes of it: every item, taken at the call |
| `Waker`, taken by value | `<prefix>_waker`, as a future's poll takes it |
| `Range<T>`, `Bound<T>` of an integer | `<prefix>_range_<T>`, `<prefix>_bound_<T>`: a `<prefix>_bound` kind and a value |
| `impl RangeBounds<T>` of an integer | `<prefix>_bounds_<T>`: its start and end bounds |
Expand Down
17 changes: 17 additions & 0 deletions ffi/fixtures/api/examples/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ fn main() {
&["a".to_string(), "b".to_string()],
);
println!("trail {:?} {:?}", trail.points(), trail.labels());
// Returned iterators, each collected at the call: drained labels are gone after.
let mut trail = trail;
println!(
"iterated {:?} {:?} {:?}",
trail.iter().collect::<Vec<_>>(),
trail.names().collect::<Vec<_>>(),
trail.bytes().collect::<Vec<_>>()
);
println!(
"drained {:?} then {:?}",
trail.drain().collect::<Vec<_>>(),
trail.labels()
);
let numbered: Vec<String> = api_lib::numbered(vec!["p".to_string(), "q".to_string()])
.map(|c| format!("{} {}", c.name(), c.get()))
.collect();
println!("numbered {}", numbered.join(", "));
let (v4, v6) = api_lib::nexts("127.0.0.1:8080".parse().unwrap(), "[::1%3]:443".parse().unwrap());
println!("nexts {} {} {} {}", v4.ip(), v4.port(), v6.port(), v6.scope_id());
}
34 changes: 34 additions & 0 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ pub const GREETINGS: &[&str] = &["hi", "hello"];
pub struct Trail {
points: Vec<Point>,
labels: Vec<Label>,
route: String,
}

impl Trail {
Expand All @@ -1111,6 +1112,7 @@ impl Trail {
Self {
points: points.to_vec(),
labels,
route: names.join("/"),
}
}

Expand All @@ -1123,6 +1125,38 @@ impl Trail {
pub fn labels(&self) -> &[Label] {
&self.labels
}

/// Its points one by one, as moq's `Hops::iter` returns them: an array of copies.
pub fn iter(&self) -> std::slice::Iter<'_, Point> {
self.points.iter()
}

/// Each name, split from its route, through an iterator only its bounds name.
pub fn names(&self) -> impl Iterator<Item = &str> {
// Splitting the empty route would yield one empty name.
(!self.route.is_empty())
.then(|| self.route.split('/'))
.into_iter()
.flatten()
}

/// The bytes of its route, which cross as a buffer.
pub fn bytes(&self) -> std::str::Bytes<'_> {
self.route.bytes()
}

/// Its labels, drained: every one is taken at the call, so none are left.
pub fn drain(&mut self) -> std::vec::Drain<'_, Label> {
self.labels.drain(..)
}
}

/// A counter for each name, counting from its index, last first.
pub fn numbered(names: Vec<String>) -> impl Iterator<Item = Counter> {
let counters: Vec<_> = (names.iter().enumerate())
.map(|(i, name)| Counter::new(name, i as u64))
.collect();
counters.into_iter().rev()
}

/// Each address at the next port, one IPv4 and one IPv6.
Expand Down
21 changes: 21 additions & 0 deletions ffi/fixtures/reject/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,30 @@ fixtures/reject/src/lib.rs:347:1: rust2-ffi: cannot export fn `stash`: generic p
fixtures/reject/src/lib.rs:362:1: rust2-ffi: cannot export fn `hold`: generic parameter `std::convert::Into`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:367:1: rust2-ffi: cannot export fn `bound`: generic parameter `std::convert::Into`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:372:1: rust2-ffi: cannot export fn `kept`: generic parameter `std::convert::Into`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:377:1: rust2-ffi: cannot export fn `alphabet`: Range `char`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:382:1: rust2-ffi: cannot export fn `naturals`: foreign type `std::ops::RangeFrom`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:387:1: rust2-ffi: cannot export fn `opaques`: borrowed return that is not Clone `Opaque`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:392:1: rust2-ffi: cannot export fn `tried`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:397:1: rust2-ffi: cannot export fn `eventually`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:402:1: rust2-ffi: cannot export trait `Source`: callback signature `items`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:407:1: rust2-ffi: cannot export fn `ones`: foreign type `std::iter::Repeat`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:40:1: rust2-ffi: cannot export fn `peek`: reference to a handle inside a value `std::boxed::Box<Opaque>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:412:1: rust2-ffi: cannot export fn `words`: foreign type `std::str::Split`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:429:1: rust2-ffi: cannot export fn `launched`: foreign type `std::iter::Chain`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:446:1: rust2-ffi: cannot export fn `launches`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:45:1: rust2-ffi: cannot export static `COUNT`: static; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:469:1: rust2-ffi: cannot export fn `pumped`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:474:1: rust2-ffi: cannot export struct `Token`: object no export returns; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:483:1: rust2-ffi: cannot export fn `skipped`: foreign type `std::iter::Skip`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:488:1: rust2-ffi: cannot export fn `merged`: foreign type `std::iter::Copied`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:495:1: rust2-ffi: cannot export fn `values`: foreign type `std::collections::btree_map::IntoValues`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:49:1: rust2-ffi: cannot export macro `noop`: macro; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:500:1: rust2-ffi: cannot export fn `zeros`: foreign type `std::iter::RepeatN`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:505:1: rust2-ffi: cannot export fn `successes`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:525:1: rust2-ffi: cannot export fn `allocated`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:532:1: rust2-ffi: cannot export fn `flattened`: foreign type `std::iter::Flatten`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:556:1: rust2-ffi: cannot export fn `lent`: foreign type `std::vec::IntoIter`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:574:1: rust2-ffi: cannot export fn `halves`: foreign type `std::str::Split`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:57:1: rust2-ffi: cannot export fn `excluded`: excluded type `Excluded`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:68:2: rust2-ffi: fn `reject_fixture::audio::encoder_finish` and fn `reject_fixture::audio_encoder::finish` are both `reject_audio_encoder_finish` in C
fixtures/reject/src/lib.rs:63:2: note: fn `reject_fixture::audio_encoder::finish` is here
Expand Down
204 changes: 203 additions & 1 deletion ffi/fixtures/reject/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Items rust2-ffi cannot export: each is a compile error at its span.

#![feature(allocator_api, pattern)]
use std::collections::HashMap;

/// A generic type.
Expand Down Expand Up @@ -372,3 +372,205 @@ pub fn bound<'a: 'static>(value: impl Into<Held<'a>>) -> u32 {
pub fn kept<'a>(value: impl Into<Held<'a>> + 'static) -> u32 {
*value.into().0
}

/// A range is a value, not an iterator to collect: one that maps to no range is refused.
pub fn alphabet() -> std::ops::Range<char> {
'a'..'z'
}

/// A range with no end, which collecting would never finish.
pub fn naturals() -> std::ops::RangeFrom<u64> {
0..
}

/// An iterator of handles with no clone to return.
pub fn opaques() -> std::slice::Iter<'static, Opaque> {
[].iter()
}

/// An iterator inside a `Result`, which crosses only as a whole return.
pub fn tried() -> Result<std::vec::IntoIter<u8>, String> {
Ok(Vec::new().into_iter())
}

/// An iterator a future resolves to.
pub async fn eventually() -> std::vec::IntoIter<u8> {
Vec::new().into_iter()
}

/// A callback returning an iterator, which C would have to make.
pub trait Source: Send + Sync {
fn items(&self) -> std::vec::IntoIter<u8>;
}

/// One byte forever.
pub fn ones() -> std::iter::Repeat<u8> {
std::iter::repeat(1)
}

/// Words split where a closure says, which a caller who stops early never runs on the rest.
pub fn words() -> impl Iterator<Item = &'static str> {
"a b".split(|c: char| c.is_whitespace())
}

/// Counts down to one, as its own code reads it.
struct Countdown(u8);

impl Iterator for Countdown {
type Item = u8;

fn next(&mut self) -> Option<u8> {
self.0 = self.0.checked_sub(1)?;
Some(self.0 + 1)
}
}

/// A countdown inside core's adapter, which still runs its own code.
pub fn launched() -> impl Iterator<Item = u8> {
Countdown(3).chain([0])
}

/// A countdown to begin from, which turns into one only as it is flattened.
struct Start(u8);

impl IntoIterator for Start {
type Item = u8;
type IntoIter = Countdown;

fn into_iter(self) -> Countdown {
Countdown(self.0)
}
}

/// Countdowns, each made by its own code as `flatten` reaches it.
pub fn launches() -> impl Iterator<Item = u8> {
vec![Start(1), Start(2)].into_iter().flatten()
}

/// How often the code below ran.
static RAN: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

/// A pump, which runs its own code each time a borrow of it is turned into an iterator.
struct Pump([u8; 1]);

impl<'a> IntoIterator for &'a Pump {
type Item = &'a u8;
type IntoIter = std::slice::Iter<'a, u8>;

fn into_iter(self) -> std::slice::Iter<'a, u8> {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
self.0.iter()
}
}

static PUMPS: [Pump; 1] = [Pump([1])];

/// Each pump's bytes, through code only the items' borrows name.
pub fn pumped() -> impl Iterator<Item = &'static u8> {
PUMPS.iter().flatten()
}

/// A token that counts its own drop.
pub struct Token(pub u8);

impl Drop for Token {
fn drop(&mut self) {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
}

/// The second token: `skip` drops the first only as the iterator is read.
pub fn skipped() -> impl Iterator<Item = Token> {
vec![Token(1), Token(2)].into_iter().skip(1)
}

/// The bytes in either set, which `union` compares as the iterator is read.
pub fn merged() -> impl Iterator<Item = u8> {
static A: std::collections::BTreeSet<u8> = std::collections::BTreeSet::new();
static B: std::collections::BTreeSet<u8> = std::collections::BTreeSet::new();
A.union(&B).copied()
}

/// A map's values, which drops each key as the iterator is read.
pub fn values() -> std::collections::btree_map::IntoValues<u8, u8> {
std::collections::BTreeMap::new().into_values()
}

/// As many zeros as the caller asks, held nowhere until made.
pub fn zeros(n: usize) -> std::iter::RepeatN<u8> {
std::iter::repeat_n(0, n)
}

/// The successes, where turning each failure into an iterator drops its token.
pub fn successes() -> impl Iterator<Item = u8> {
vec![Err(Token(0)), Ok(7)].into_iter().flatten()
}

/// An allocator of the crate's own, which counts each call into it.
struct Counted;

unsafe impl std::alloc::Allocator for Counted {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
std::alloc::Global.allocate(layout)
}

unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
unsafe { std::alloc::Global.deallocate(ptr, layout) }
}
}

/// Bytes in the crate's own allocation, which collecting frees through its allocator.
pub fn allocated() -> impl Iterator<Item = u8> {
let mut bytes = Vec::new_in(Counted);
bytes.push(1);
bytes.into_iter()
}

/// The same bytes, whose allocation `flatten` frees through the allocator as it reads them.
pub fn flattened() -> impl Iterator<Item = u8> {
let mut bytes = Vec::new_in(Counted);
bytes.push(1);
vec![bytes].into_iter().flatten()
}

/// An allocator of the crate's own, which only a borrow of it implements.
struct Local;

unsafe impl std::alloc::Allocator for &Local {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
std::alloc::Global.allocate(layout)
}

unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
RAN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
unsafe { std::alloc::Global.deallocate(ptr, layout) }
}
}

static LOCAL: Local = Local;

/// Bytes in an allocation a borrowed allocator frees as collecting finishes.
pub fn lent() -> impl Iterator<Item = u8> {
let mut bytes = Vec::new_in(&LOCAL);
bytes.push(1);
bytes.into_iter()
}

/// A pattern of the crate's own, whose searcher a split runs as it is read.
struct Slash;

impl std::str::pattern::Pattern for Slash {
type Searcher<'a> = std::str::pattern::CharSearcher<'a>;

fn into_searcher(self, haystack: &str) -> std::str::pattern::CharSearcher<'_> {
'/'.into_searcher(haystack)
}
}

/// Halves split where the crate's own pattern says.
pub fn halves() -> impl Iterator<Item = &'static str> {
"a/b".split(Slash)
}
5 changes: 1 addition & 4 deletions ffi/moq/ffi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ exclude = [
"Consume",
"DecodeError",
"Driver",
"Hops::iter",
"IntoBytes",
"InvalidEpoch",
"Path::from_static",
"Path::parts",
"Server::accept",
"Server::accept_request",
"Timed",
"Timestamp::as_micros",
"Timestamp::as_millis",
"Timestamp::as_nanos",
"Timestamp::as_scale",
"Versions::iter",
"announce::Announce",
"announce::Consumer::poll_synced",
"auth::EmptyToken",
Expand Down Expand Up @@ -83,7 +80,7 @@ exclude = [

# Exported with moq-net, whose signatures name their types, such as `Patterns` and `Instant`.
[crates.moq-pattern]
exclude = ["Pattern::captures", "Patterns::iter"]
exclude = ["Pattern::captures"]

[crates.moq-clock]
exclude = [
Expand Down
Loading