tools/corrosion/test/cbindgen/rust2cpp/rust/src/lib.rs
author unC0Rr
Wed, 20 Nov 2024 21:37:47 +0100
branchtransitional_engine
changeset 16067 d903f8d2395a
parent 16050 6a3dc15b78b9
permissions -rw-r--r--
- Update corrosion - Implement installation of targets imported with corrosion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16050
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     1
pub const MAGIC_NUMBER: u64 = 0xABCD_EFAB;
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     2
16067
d903f8d2395a - Update corrosion
unC0Rr
parents: 16050
diff changeset
     3
pub mod ffi;
d903f8d2395a - Update corrosion
unC0Rr
parents: 16050
diff changeset
     4
pub mod other_mod;
d903f8d2395a - Update corrosion
unC0Rr
parents: 16050
diff changeset
     5
16050
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     6
#[derive(Debug)]
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     7
#[repr(C)]
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     8
pub struct Point {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
     9
    x: u64,
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    10
    y: u64,
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    11
}
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    12
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    13
impl Point {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    14
    pub(crate) fn add(&mut self, rhs: &Point) {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    15
        self.x = self.x.wrapping_add(rhs.x);
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    16
        self.y = self.y.wrapping_add(rhs.y);
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    17
    }
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    18
}
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    19
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    20
#[no_mangle]
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    21
pub extern "C" fn add_point(lhs: Option<&mut Point>, rhs: Option<&Point>) {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    22
    if let (Some(p1), Some(p2)) = (lhs, rhs) {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    23
        p1.add(p2);
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    24
        // Print something so we can let Ctest assert the output.
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    25
        println!("add_point Result: {:?}", p1);
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    26
    }
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    27
}
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    28
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    29
// simple test if the constant was exported by cbindgen correctly
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    30
#[no_mangle]
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    31
pub extern "C" fn is_magic_number(num: u64) -> bool {
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    32
    num == MAGIC_NUMBER
6a3dc15b78b9 Add corrosion as a subdirectory, CMake fixes
unC0Rr
parents:
diff changeset
    33
}