homework done
This commit is contained in:
6
smart-house/use_static/Cargo.toml
Normal file
6
smart-house/use_static/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "use_static"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
4
smart-house/use_static/build.rs
Normal file
4
smart-house/use_static/build.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println!("cargo:rustc-link-lib=static=power_socket_lib");
|
||||
println!("cargo:rustc-link-search=target/debug");
|
||||
}
|
||||
34
smart-house/use_static/src/main.rs
Normal file
34
smart-house/use_static/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
mod ps {
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct PowerSocket {
|
||||
power_rate: f32,
|
||||
on: bool,
|
||||
}
|
||||
// #[link(name = "power_socket_lib", kind = "static")]
|
||||
unsafe extern "C" {
|
||||
pub fn power_socket_new(power_rate: f32, on: bool) -> PowerSocket;
|
||||
|
||||
pub fn power_socket_is_on(power_socket: &PowerSocket) -> bool;
|
||||
|
||||
pub fn power_socket_set_on(power_socket: &mut PowerSocket, on: bool);
|
||||
|
||||
pub fn power_socket_get_power(power_socket: &PowerSocket) -> f32;
|
||||
}
|
||||
}
|
||||
|
||||
use ps::*;
|
||||
|
||||
fn main() {
|
||||
let mut power_socket = unsafe { power_socket_new(12.0, false) };
|
||||
println!("call power_socket_new -> {:?}", power_socket);
|
||||
|
||||
println!("call power_socket_is_on -> {:?}", unsafe { power_socket_is_on(&power_socket) });
|
||||
println!("call power_socket_get_power -> {:?}", unsafe { power_socket_get_power(&power_socket) });
|
||||
|
||||
unsafe { power_socket_set_on(&mut power_socket, true) };
|
||||
println!("call power_socket_set_on(&ref, true)");
|
||||
|
||||
println!("call power_socket_is_on -> {:?}", unsafe { power_socket_is_on(&power_socket) });
|
||||
println!("call power_socket_get_power -> {:?}", unsafe { power_socket_get_power(&power_socket) });
|
||||
}
|
||||
Reference in New Issue
Block a user