Skip to content

Commit 29ffbab

Browse files
authored
Pass library types through function pointers (#657)
1 parent d4df783 commit 29ffbab

11 files changed

Lines changed: 160 additions & 7 deletions

File tree

‎libcc2rs/build.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() {
4141
);
4242

4343
buf.push_str(&format!(
44-
"#[path = r#\"{}\"#]\nmod {};\npub use {}::*;\n",
44+
"#[path = r#\"{}\"#]\nmod {};\n#[allow(unused_imports)]\npub use {}::*;\n",
4545
shim.display(),
4646
module_name,
4747
module_name

‎libcc2rs/src/reinterpret.rs‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ impl ByteRepr for bool {
116116
}
117117

118118
impl ByteRepr for () {}
119-
impl ByteRepr for std::fs::File {}
120-
impl<T: ByteRepr> ByteRepr for Vec<T> {}
121119
impl<T: ByteRepr> ByteRepr for Option<T> {}
122120
// The bytes of a Value, like a field stored in one, are those of its value.
123121
impl<T: ByteRepr> ByteRepr for std::rc::Rc<T> {
@@ -192,9 +190,6 @@ impl<T: ByteRepr> ByteRepr for Box<[T]> {
192190
impl<T: ByteRepr> ByteRepr for Box<T> {}
193191
impl<T: 'static> ByteRepr for *const T {}
194192
impl<T: 'static> ByteRepr for *mut T {}
195-
impl<A: ByteRepr, B: ByteRepr> ByteRepr for (A, B) {}
196-
impl<K: 'static, V: 'static> ByteRepr for std::collections::BTreeMap<K, V> {}
197-
impl ByteRepr for std::rc::Rc<dyn std::any::Any> {}
198193

199194
// Runs `f` with a zeroed scratch buffer of `len` elements. Small buffers live
200195
// on the stack so that accessing memory through a reinterpreted pointer does

‎rules/any/shim.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::ByteRepr;
5+
6+
impl ByteRepr for std::rc::Rc<dyn std::any::Any> {}

‎rules/compare/shim.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr};
5+
6+
impl ByteRepr for std::cmp::Ordering {}
7+
8+
impl FnPtrArg for std::cmp::Ordering {
9+
#[inline]
10+
fn to_repr(self) -> ArgRepr {
11+
ArgRepr::Record(Box::new(self))
12+
}
13+
fn from_repr(r: ArgRepr) -> Self {
14+
record_from_repr(r)
15+
}
16+
}

‎rules/fstream/shim.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::ByteRepr;
5+
6+
impl ByteRepr for std::fs::File {}

‎rules/map/shim.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
UnsafeBidirectionalIterator, UnsafeCppIterator, Value, cpp_cmp,
1313
};
1414

15+
impl<K: 'static, V: 'static> ByteRepr for BTreeMap<K, V> {}
16+
1517
// A map keeps a single copy of each key, as C++ does, since copying a key may
1618
// have side effects. Its index points to the keys of the entries, is ordered
1719
// by them with operator< (CppLess), and is searched for a key through a

‎rules/pair/shim.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
use crate::Value;
4+
use crate::{ArgRepr, ByteRepr, FnPtrArg, Value, record_from_repr};
55

66
pub trait TupleElement<const N: usize> {
77
type Type;
@@ -76,3 +76,15 @@ macro_rules! impl_tuple_get {
7676
}
7777

7878
impl_tuple_get!((T0, T1); 0 T0, 1 T1);
79+
80+
impl<A: ByteRepr, B: ByteRepr> ByteRepr for (A, B) {}
81+
82+
impl<A: 'static, B: 'static> FnPtrArg for (A, B) {
83+
#[inline]
84+
fn to_repr(self) -> ArgRepr {
85+
ArgRepr::Record(Box::new(self))
86+
}
87+
fn from_repr(r: ArgRepr) -> Self {
88+
record_from_repr(r)
89+
}
90+
}

‎rules/vector/shim.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr};
5+
6+
impl<T: ByteRepr> ByteRepr for Vec<T> {}
7+
8+
impl<T: 'static> FnPtrArg for Vec<T> {
9+
#[inline]
10+
fn to_repr(self) -> ArgRepr {
11+
ArgRepr::Record(Box::new(self))
12+
}
13+
fn from_repr(r: ArgRepr) -> Self {
14+
record_from_repr(r)
15+
}
16+
}

‎tests/unit/fn_ptr.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
12
#include <assert.h>
3+
#include <compare>
24
#include <cstddef>
35
#include <signal.h>
46
#include <stddef.h>
7+
#include <string>
8+
#include <utility>
9+
#include <vector>
510

611
typedef int (*foo_t)(void *);
712

@@ -33,6 +38,14 @@ static int total = 0;
3338

3439
void add_to_total(int x) { total += x; }
3540

41+
int pair_sum(std::pair<int, int> p) { return p.first + p.second; }
42+
43+
size_t string_size(std::string s) { return s.size(); }
44+
45+
int vector_sum(std::vector<int> v) { return v[0] + v[1]; }
46+
47+
bool is_less(std::strong_ordering o) { return o < 0; }
48+
3649
int main() {
3750
foo_t fn = nullptr;
3851
assert(fn == nullptr);
@@ -65,5 +78,22 @@ int main() {
6578
handler(2);
6679
handler(3);
6780
assert(total == 5);
81+
82+
int (*pf)(std::pair<int, int>) = pair_sum;
83+
assert(pf({1, 2}) == 3);
84+
85+
size_t (*sf)(std::string) = string_size;
86+
assert(sf(std::string("abc")) == 3);
87+
88+
int (*vf)(std::vector<int>) = vector_sum;
89+
assert(vf({1, 2}) == 3);
90+
91+
bool (*of)(std::strong_ordering) = is_less;
92+
assert(of(1 <=> 2));
93+
assert(!of(2 <=> 1));
94+
95+
std::strong_ordering o = 1 <=> 2;
96+
std::strong_ordering *op = &o;
97+
assert(*op == std::strong_ordering::less);
6898
return 0;
6999
}

‎tests/unit/out/refcount/fn_ptr.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ thread_local!(
4747
pub fn add_to_total_8(mut x: i32) {
4848
total_7.with(|rc| *rc.borrow_mut() += x);
4949
}
50+
pub fn pair_sum_9(p: (Value<i32>, Value<i32>)) -> i32 {
51+
let p: Value<(Value<i32>, Value<i32>)> = Rc::new(RefCell::new(p));
52+
return ((*(*p.borrow()).0.borrow()) + (*(*p.borrow()).1.borrow()));
53+
}
54+
pub fn string_size_10(mut s: Vec<i8>) -> usize {
55+
return (s.len() - 1);
56+
}
57+
pub fn vector_sum_11(mut v: Vec<i32>) -> i32 {
58+
return (v[0_usize] + v[1_usize]);
59+
}
60+
pub fn is_less_12(o: std::cmp::Ordering) -> bool {
61+
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(o));
62+
return ({ (*o.borrow()) }) == std::cmp::Ordering::Less;
63+
}
5064
pub fn main() {
5165
__cpp2rust_init_globals();
5266
std::process::exit(main_0());
@@ -82,6 +96,28 @@ fn main_0() -> i32 {
8296
({ handler.call(2) });
8397
({ handler.call(3) });
8498
assert!((total_7.with(|rc| *rc.borrow()) == 5));
99+
let mut pf: FnPtr<fn((Value<i32>, Value<i32>)) -> i32> =
100+
FnPtr::<fn((Value<i32>, Value<i32>)) -> i32>::new(pair_sum_9);
101+
assert!((({ pf.call((Rc::new(RefCell::new(1)), Rc::new(RefCell::new(2))),) }) == 3));
102+
let mut sf: FnPtr<fn(Vec<i8>) -> usize> = FnPtr::<fn(Vec<i8>) -> usize>::new(string_size_10);
103+
assert!(
104+
(({
105+
sf.call({
106+
let mut __bytes = Ptr::<i8>::from_string_literal(b"abc").to_c_bytes();
107+
__bytes.push(0);
108+
__bytes
109+
})
110+
}) == 3_usize)
111+
);
112+
let mut vf: FnPtr<fn(Vec<i32>) -> i32> = FnPtr::<fn(Vec<i32>) -> i32>::new(vector_sum_11);
113+
assert!((({ vf.call(Vec::from([1, 2,]),) }) == 3));
114+
let mut of: FnPtr<fn(std::cmp::Ordering) -> bool> =
115+
FnPtr::<fn(std::cmp::Ordering) -> bool>::new(is_less_12);
116+
assert!(({ of.call(std::cmp::Ord::cmp(&(1), &(2)),) }));
117+
assert!(!({ of.call(std::cmp::Ord::cmp(&(2), &(1)),) }));
118+
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
119+
let mut op: Ptr<std::cmp::Ordering> = (o.as_pointer());
120+
assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less);
85121
return 0;
86122
}
87123
pub fn __cpp2rust_init_globals() {

0 commit comments

Comments
 (0)