1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #[cxx::bridge]
6 mod ffi {
7 pub struct SomeStruct {
8 a: i32,
9 }
10 extern "Rust" {
say_hello()11 fn say_hello();
allocate_via_rust() -> Box<SomeStruct>12 fn allocate_via_rust() -> Box<SomeStruct>;
add_two_ints_via_rust(x: i32, y: i32) -> i3213 fn add_two_ints_via_rust(x: i32, y: i32) -> i32;
14 }
15 }
16
say_hello()17 pub fn say_hello() {
18 println!(
19 "Hello, world - from a Rust library. Calculations suggest that 3+4={}",
20 add_two_ints_via_rust(3, 4)
21 );
22 }
23
24 #[test]
test_hello()25 fn test_hello() {
26 assert_eq!(7, add_two_ints_via_rust(3, 4));
27 }
28
add_two_ints_via_rust(x: i32, y: i32) -> i3229 pub fn add_two_ints_via_rust(x: i32, y: i32) -> i32 {
30 x + y
31 }
32
33 // The next function is used from the
34 // AllocatorTest.RustComponentUsesPartitionAlloc unit test.
allocate_via_rust() -> Box<ffi::SomeStruct>35 pub fn allocate_via_rust() -> Box<ffi::SomeStruct> {
36 Box::new(ffi::SomeStruct { a: 43 })
37 }
38
39 mod tests {
40 #[test]
test_in_mod()41 fn test_in_mod() {
42 // Always passes; just to see if tests in modules are handled correctly.
43 }
44 }
45