1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 use ffi::{GetUniqueSptr, KeepUniqueSptr};
15
16 #[cxx::bridge(namespace = "OHOS")]
17 mod ffi {
18 unsafe extern "C++" {
19 include!("samgr_rust_test_mem.h");
20 type Keeper;
21 type SptrFoo;
GetUniqueSptr() -> UniquePtr<SptrFoo>22 fn GetUniqueSptr() -> UniquePtr<SptrFoo>;
KeepUniqueSptr(s: UniquePtr<SptrFoo>) -> UniquePtr<Keeper>23 fn KeepUniqueSptr(s: UniquePtr<SptrFoo>) -> UniquePtr<Keeper>;
24 }
25 }
26
27 #[test]
test()28 fn test() {
29 let w = GetUniqueSptr();
30 unsafe {
31 assert_eq!(g_testDrop, 0);
32 }
33 drop(w);
34 unsafe {
35 assert_eq!(g_testDrop, 1);
36 }
37 let w = GetUniqueSptr();
38 let keeper = KeepUniqueSptr(w);
39 unsafe {
40 assert_eq!(g_testDrop, 1);
41 }
42 drop(keeper);
43 unsafe {
44 assert_eq!(g_testDrop, 2);
45 }
46 }
47
48 #[link(name = "samgr_rust_test_c")]
49 extern "C" {
50 static g_testDrop: i32;
51 }
52