xref: /build/rust/tests/test_cxx/src/lib.rs (revision 5f9996aa)
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16//! #[cxx::bridge]
17#[cxx::bridge]
18mod ffi{
19    #![allow(dead_code)]
20    #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
21    struct Shared {
22        z: usize,
23    }
24    extern "Rust"{
25        fn print_message_in_rust();
26        fn r_return_primitive() -> usize;
27        fn r_return_shared() -> Shared;
28        fn r_return_rust_string() -> String;
29        fn r_return_sum(_: usize, _: usize) -> usize;
30    }
31}
32
33fn print_message_in_rust(){
34    println!("Here is a test for cpp call Rust.");
35}
36fn r_return_shared() -> ffi::Shared {
37    println!("Here is a message from Rust,test for ffi::Shared:");
38    ffi::Shared { z: 1996 }
39}
40fn r_return_primitive() -> usize {
41    println!("Here is a message from Rust,test for usize:");
42    1997
43}
44fn r_return_rust_string() -> String {
45    println!("Here is a message from Rust,test for String");
46    "Hello World!".to_owned()
47}
48fn r_return_sum(n1: usize, n2: usize) -> usize {
49    println!("Here is a message from Rust,test for {} + {} is:",n1 ,n2);
50    n1 + n2
51}
52