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
13 changes: 10 additions & 3 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,15 @@ packages place it in.
lives only as long as the bytes it views, and a `view` lent to a callback only for the
call; reading one past that is C++'s own undefined behavior, which Clang's `-Wdangling`
refuses for a view or `str()` of a temporary `buf`. A `Vec<T>` is a `std::vector`, passed in place
when of a fixed-width integer other than `u8` (bytes) or `u128` or of a float, of `view`s for bytes passed in, a socket address `socket_addr`, the C struct, and a `u128`
`u128`, the C struct. `Option<T>` is `std::optional<T>`, a tuple `std::tuple`, and `Result<T, E>` a `result<T, E>`, whose `value()` aborts on an
when of a fixed-width integer other than `u8` (bytes) or `u128` or of a float. One whose
items hold bytes is taken as `items` of `view`s, which points into a `std::vector` of them
in place and converts any other range whose items convert and outlive the call, such as a
returned `std::vector<buf>`, but not a range of temporaries. A `std::vector` moved in
converts with each item moved, so a returned one passes back even when its items hold a
`Copy` object C++ cannot copy. One whose items also move a
handle is taken as `taken`, a `std::vector` of them moved in as it is, or any other such
range moved in, each handle moved out of it. A socket address is a `socket_addr` and a
`u128` a `u128`, each the C struct. `Option<T>` is `std::optional<T>`, a tuple `std::tuple`, and `Result<T, E>` a `result<T, E>`, whose `value()` aborts on an
error. A `[u8; N]` is a `std::array<std::uint8_t, N>`.
- **Traits** are abstract classes, passed as a `std::unique_ptr` that Rust deletes.
- **Closures** are any callable, taken by a template parameter, which Rust calls from any
Expand Down Expand Up @@ -510,7 +517,7 @@ out, with only its other fields encoded.
target's range, `Option<T>` `T?`, a tuple `Pair` or
`Triple`, or else a generated `Tuple<n>`, and also `Tuple2` or `Tuple3` when it holds a
numeric array or a float anywhere, records included, since `Pair` and `Triple` cannot
compare them as Rust does (so a `BTreeMap<String, f64>` is a `List<Tuple2<String, Double>>`), though a tuple of one returned whole is its element, `u128` `java.math.BigInteger`, which throws `IllegalArgumentException` passed in out of its range, `Duration` `kotlin.time.Duration`, and an `Err` a thrown `<E>Exception`
compare them as Rust does (so a `BTreeMap<String, f64>` is a `List<Tuple2<String, Double>>`), `u128` `java.math.BigInteger`, which throws `IllegalArgumentException` passed in out of its range, `Duration` `kotlin.time.Duration`, and an `Err` a thrown `<E>Exception`
holding it, whose message is its `Display`. A `[u8; N]` is a generated `ArrayU8x<N>`,
which copies the `ByteArray` it is made of and throws `IllegalArgumentException` unless
it holds exactly `N` bytes.
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 @@ -116,6 +116,8 @@ fn main() {
("low".to_string(), Counter::new("l", 1)),
("high".to_string(), Counter::new("h", 9)),
]);
// The counters a board returns build another, each handle moved back in.
let board = api::Board::new(board.into_counters());
let ranked: Vec<String> = (board.ranked())
.map(|(name, counter)| format!("{name} {} {}", counter.name(), counter.get()))
.collect();
Expand Down
10 changes: 10 additions & 0 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ impl Board {
}
}

/// Each name and its counter in name order, each handle moved out of the board.
pub fn into_counters(self) -> Vec<(String, Counter)> {
self.counters.into_iter().collect()
}

/// Each name and its counter, highest first, as hang's `ranked`: each pair borrowed,
/// so each crosses as a clone.
pub fn ranked(&self) -> impl Iterator<Item = (&String, &Counter)> {
Expand Down Expand Up @@ -781,6 +786,11 @@ impl Ticket {
}
}

/// Each name and its ticket, as given, so a binding passes back what it returned.
pub fn tickets(entries: Vec<(String, Ticket)>) -> Vec<(String, Ticket)> {
entries
}

/// The ticket `swap` returns for `ticket`.
pub fn reissue(ticket: Ticket, swap: impl Fn(Ticket) -> Ticket) -> Ticket {
swap(ticket)
Expand Down
100 changes: 100 additions & 0 deletions ffi/runtime/rt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,106 @@ class input {
friend struct detail::access;
};

namespace detail {
// Whether each item of the range `R` lives as long as `R` does, so a `T` made of it may view
// its bytes: a reference into `R`, or a `T` itself, but no other temporary, such as the
// `std::string` a C++20 transform may yield, which dies before the call reads it.
template <class R, class T, class Item = decltype(*::std::begin(::std::declval<R &>()))>
inline constexpr bool lasting = ::std::is_reference_v<Item> || ::std::is_same_v<::std::decay_t<Item>, T>;
} // namespace detail

// Items lent to a call whose items hold bytes, such as `view`s: a `std::vector<T>` in place,
// or any other range whose items convert to `T`, such as the `std::vector<buf>` a call
// returned or a `std::vector<std::string>`, converted item by item.
template <class T>
class items {
::std::vector<T> converted_;
const T *data_;
::std::size_t size_;

public:
items(const ::std::vector<T> &vector RUST2_FFI_LIFETIMEBOUND) noexcept : data_(vector.data()), size_(vector.size()) {}
// Copied, since a list's array may not outlive the call's first full-expression.
items(::std::initializer_list<T> list) : converted_(list), data_(converted_.data()), size_(converted_.size()) {}
template <class R,
class = ::std::enable_if_t<!::std::is_same_v<::std::decay_t<R>, ::std::vector<T>> &&
!::std::is_same_v<::std::decay_t<R>, items> && detail::lasting<const R, T> &&
::std::is_convertible_v<decltype(*::std::begin(::std::declval<const R &>())), T>>>
items(const R &range RUST2_FFI_LIFETIMEBOUND) {
// A loop, since a C++20 range's end may be a sentinel of another type than its begin.
for (auto &&item : range) {
converted_.emplace_back(item);
}
data_ = converted_.data();
size_ = converted_.size();
}
// A vector moved in, each item moved: a returned `std::vector<std::tuple<buf, H>>` whose
// `H` is a `Copy` object with no C++ copy. Only an owning vector, since a moved view's
// items belong to the caller.
template <class U, class = ::std::enable_if_t<!::std::is_same_v<U, T> && ::std::is_convertible_v<U &&, T>>>
items(::std::vector<U> &&vector RUST2_FFI_LIFETIMEBOUND) {
converted_.reserve(vector.size());
for (auto &item : vector) {
converted_.emplace_back(::std::move(item));
}
data_ = converted_.data();
size_ = converted_.size();
}
// A copy would point into the original's converted items.
items(const items &) = delete;
items &operator=(const items &) = delete;
// A moved vector keeps its array, so the pointer stays valid.
items(items &&other) noexcept = default;

const T *begin() const noexcept {
return data_;
}
const T *end() const noexcept {
return data_ + size_;
}
::std::size_t size() const noexcept {
return size_;
}
bool empty() const noexcept {
return size_ == 0;
}
};

// Items moved into a call whose items hold both bytes, as `view`s, and handles it takes: a
// `std::vector<T>` as it is, or any other range moved in whose items convert to `T`, such as
// the `std::vector<std::tuple<buf, H>>` a call returned, each handle moved out of it. Its
// views point into the range's bytes, which live until the call's full-expression ends.
template <class T>
class taken {
::std::vector<T> items_;

public:
taken() noexcept = default;
taken(::std::vector<T> &&vector) noexcept : items_(::std::move(vector)) {}
// A range moved in, never one lent: its handles move out.
template <class R, class = ::std::enable_if_t<!::std::is_reference_v<R> && !::std::is_same_v<R, ::std::vector<T>> &&
!::std::is_same_v<R, taken> && detail::lasting<R, T> &&
::std::is_convertible_v<decltype(::std::move(*::std::begin(::std::declval<R &>()))), T>>>
taken(R &&range RUST2_FFI_LIFETIMEBOUND) {
for (auto &&item : range) {
items_.emplace_back(::std::move(item));
}
}

T *begin() noexcept {
return items_.data();
}
T *end() noexcept {
return items_.data() + items_.size();
}
::std::size_t size() const noexcept {
return items_.size();
}
bool empty() const noexcept {
return items_.empty();
}
};

// What a fallible call returns: its value, or the error that stopped it.
template <class T, class E>
class result {
Expand Down
2 changes: 2 additions & 0 deletions ffi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,8 @@ impl<'a> Abi<'a> {
"buf_clone",
"poll",
"view",
"items",
"taken",
"borrow",
"result",
"task",
Expand Down
12 changes: 10 additions & 2 deletions ffi/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,19 @@ impl<'a> Header<'a> {
}
}

/// The C++ vector a `Vec` parameter of type `ty` takes: of views, for bytes.
/// The C++ type a `Vec` parameter of type `ty` takes: a vector, or, when its items hold
/// views, the runtime's `items` lent or `taken` when they move handles, each of which
/// also takes a returned vector of `buf`s as it is.
fn items(&self, ty: &Type) -> String {
let Type::List(_, inner) = ty else {
unreachable!("{ty:?} is no Vec")
};
format!("::std::vector<{}>", self.held(inner))
let held = self.held(inner);
match (held == self.owned(inner), moves(self.abi, inner)) {
(true, _) => format!("::std::vector<{held}>"),
(false, true) => format!("::{}::taken<{held}>", self.ns),
(false, false) => format!("::{}::items<{held}>", self.ns),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

/// The C++ type a parameter's `Vec`, `Option`, or tuple holds a `ty` as: bytes as a view.
Expand Down Expand Up @@ -507,6 +514,7 @@ impl<'a> Header<'a> {
"cstdio",
"cstdlib",
"cstring",
"initializer_list",
"ios",
"memory",
"mutex",
Expand Down
27 changes: 19 additions & 8 deletions ffi/src/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,13 +2098,20 @@ impl<'a> Gen<'a> {
let rust_args: Vec<String> = args.iter().flat_map(|a| a.rust_args.clone()).collect();
let after: Vec<String> = args.iter().flat_map(|a| a.after.clone()).collect();

// A tuple of one returned whole is its element, written through its one out pointer;
// one behind a reference or `Box` is returned as a struct, so stays a tuple.
// A tuple of one returned whole crosses as its element, written through its one out
// pointer, and is wrapped back into the tuple class its parameters take; one behind a
// reference or `Box` is returned as a struct, so crosses as a tuple.
let single = match &function.ok {
Type::Tuple(elements) if elements.len() == 1 => Some(self.tuple_class(elements)),
_ => None,
};
let ok = match &function.ok {
Type::Tuple(elements) if elements.len() == 1 => strip_refs(&elements[0]),
ok => strip_refs(ok),
};
let ret_kt = self.kt(&ok);
let ret_kt = self.kt(&strip_refs(&function.ok));
// What crosses: a tuple of one's element, before it is wrapped.
let ok_kt = self.kt(&ok);
let err = function.err.as_ref().map(strip_refs);
let exception = err.as_ref().map(|e| self.exception(e));
// A record returned directly crosses as its scalars, set into slots the call takes,
Expand Down Expand Up @@ -2215,7 +2222,7 @@ impl<'a> Gen<'a> {
value = if ok == Type::Unit {
format!("{awaited}.let {{ Unit }}")
} else {
format!("({awaited}) as {ret_kt}")
format!("({awaited}) as {ok_kt}")
};
} else if written.is_empty()
&& matches!(&function.ok, Type::Tuple(elements) if elements.len() > 1 && elements.iter().map(|ty| match abi.ty(ty) { Ty::Int(Int::I64 | Int::U64 | Int::Isize | Int::Usize) | Ty::F64 | Ty::Handle { .. } => 2, _ => 1 }).sum::<usize>() + usize::from(err.is_some()) <= 255 && elements.iter().all(|ty| matches!(abi.ty(ty), Ty::Bool | Ty::Int(_) | Ty::F32 | Ty::F64 | Ty::Char | Ty::Enum(_) | Ty::Handle { .. })))
Expand Down Expand Up @@ -2299,7 +2306,7 @@ impl<'a> Gen<'a> {
} else {
self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; ")))
};
value = format!("({raw}) as {ret_kt}");
value = format!("({raw}) as {ok_kt}");
} else if let Some(c_ok) = direct(&outs, &written, &function.ok, function.err.as_ref()) {
// A result of a scalar, a handle, or nothing returns its value directly, raising
// its error as a `Native.Failed` Kotlin catches.
Expand Down Expand Up @@ -2414,7 +2421,7 @@ impl<'a> Gen<'a> {
};
value = match ok {
Type::Unit => format!("{raw}.let {{ Unit }}"),
_ => format!("({raw}) as {ret_kt}"),
_ => format!("({raw}) as {ok_kt}"),
};
} else if let Some(flat) = flat {
let mut params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect();
Expand Down Expand Up @@ -2490,7 +2497,7 @@ impl<'a> Gen<'a> {
} else {
self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; ")))
};
value = format!("({raw}) as {ret_kt}");
value = format!("({raw}) as {ok_kt}");
} else {
let c_ret = export.call.ret.clone();
// A block returns as the primitive array of its width, copied in one call.
Expand Down Expand Up @@ -2524,6 +2531,10 @@ impl<'a> Gen<'a> {
};
}

let value = match &single {
Some(class) => format!("{class}({value})"),
None => value,
};
// Wrap the value in each argument's holds, innermost last.
let mut code = value;
for arg in args.iter().rev() {
Expand Down Expand Up @@ -2568,7 +2579,7 @@ impl<'a> Gen<'a> {
None => "",
};
let ret = match ok {
Type::Unit => String::new(),
Type::Unit if single.is_none() => String::new(),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
_ => format!(": {ret_kt}"),
};
let property = matches!(
Expand Down
28 changes: 28 additions & 0 deletions ffi/tests/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#ifdef __cpp_lib_span
#include <span>
#endif
#ifdef __cpp_lib_ranges
#include <ranges>
#endif

#include "api.hpp"

Expand Down Expand Up @@ -336,6 +339,8 @@ int script() {
meters.emplace_back("low", api::Counter::new_("l", 1));
meters.emplace_back("high", api::Counter::new_("h", 9));
auto board = api::Board::new_(std::move(meters));
// The counters a board returns build another, each handle moved back in.
board = api::Board::new_(std::move(board).into_counters());
std::printf("ranked");
auto ranked = board.ranked();
for (std::size_t i = 0; i < ranked.size(); i++) {
Expand Down Expand Up @@ -1198,6 +1203,29 @@ int script() {
std::printf("reissued %u %u, cloned %u\n", static_cast<unsigned>(given), static_cast<unsigned>(held),
static_cast<unsigned>(api::Ticket::clones()));
}
{
// `Copy` objects with no C++ copy, beside bytes: a returned vector moves back in,
// stored or not, each ticket moved and each name viewed in place.
std::vector<std::tuple<api::view, api::Ticket>> lent;
lent.emplace_back("a", api::Ticket::new_(1));
auto returned = api::tickets(lent);
auto again = api::tickets(api::tickets(std::move(returned)));
if (again.size() != 1 || std::get<0>(again[0]).str() != "a" || std::get<1>(again[0]).number() != 1) {
std::fprintf(stderr, "returned tickets passed back wrong\n");
std::abort();
}
}
#ifdef __cpp_lib_ranges
{
// A range whose end is a sentinel of another type than its begin.
std::vector<std::string> letters{"p", "q", "r"};
auto taken = api::counters(letters | std::views::take_while([](const std::string &n) { return n != "r"; }));
if (taken.size() != 2 || taken[1].name().str() != "q") {
std::fprintf(stderr, "a sentinel range passed wrong\n");
std::abort();
}
}
#endif
// A kept closure, called on another thread after the call that took it returned.
std::printf("spawned %u\n", static_cast<unsigned>(api::Spawned::new_([] { return 7; }).join()));
// A kept closure: called again at the instant it asks for, then once its waiter wakes.
Expand Down
2 changes: 1 addition & 1 deletion ffi/tests/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fun script() {

val (below, above) = Counter.new("s", 7u).split(5u)
println("split ${below.name()} ${below.get()} ${above.name()} ${above.get()}")
val only = only(Counter.new("o", 3u))
val (only) = only(Counter.new("o", 3u))
println("only ${only.name()} ${only.get()}")
val (w8, w16, w32, w64) = widths(98305u)
println("widths $w8 $w16 $w32 $w64")
Expand Down
Loading