• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Requires this allow since cxx generates unsafe code.
6 //
7 // TODO(crbug.com/1422745): patch upstream cxx to generate compatible code.
8 #[allow(unsafe_op_in_unsafe_fn)]
9 #[cxx::bridge]
10 mod ffi {
11     pub struct SomeStruct {
12         a: i32,
13     }
14     extern "Rust" {
say_hello()15         fn say_hello();
allocate_via_rust() -> Box<SomeStruct>16         fn allocate_via_rust() -> Box<SomeStruct>;
add_two_ints_via_rust(x: i32, y: i32) -> i3217         fn add_two_ints_via_rust(x: i32, y: i32) -> i32;
18     }
19 }
20 
say_hello()21 pub fn say_hello() {
22     println!(
23         "Hello, world - from a Rust library. Calculations suggest that 3+4={}",
24         add_two_ints_via_rust(3, 4)
25     );
26 }
27 
28 #[test]
test_hello()29 fn test_hello() {
30     assert_eq!(7, add_two_ints_via_rust(3, 4));
31 }
32 
add_two_ints_via_rust(x: i32, y: i32) -> i3233 pub fn add_two_ints_via_rust(x: i32, y: i32) -> i32 {
34     x + y
35 }
36 
37 // The next function is used from the
38 // AllocatorTest.RustComponentUsesPartitionAlloc unit test.
allocate_via_rust() -> Box<ffi::SomeStruct>39 pub fn allocate_via_rust() -> Box<ffi::SomeStruct> {
40     Box::new(ffi::SomeStruct { a: 43 })
41 }
42 
43 mod tests {
44     #[test]
test_in_mod()45     fn test_in_mod() {
46         // Always passes; just to see if tests in modules are handled correctly.
47     }
48 }
49