/
LavrGov
/
csharp_ob
Обзор
Документация
Войти
/
LavrGov
/
csharp_ob
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
RunCode/mem_cpy/src/lib.rs
75 строк
2 KB
Lavrentiy
Add mem_cpy
03 июн 2025, 22:55
03 июн 2025, 22:55
7fc7f70
Код
Авторство
О чём код?
use core::ffi; use std::{ffi::c_char, os::raw::c_void}; use libc::{c_int, c_longlong, memcpy}; pub fn add(left: u64, right: u64) -> u64 { left + right } fn print_byte(data: * const u8) { unsafe { let new_slice = std::slice::from_raw_parts(data, 30); println!("{:?}", new_slice); } } #[unsafe(no_mangle)] pub extern "C" fn ffi_mem_num(dst: c_longlong, src: c_longlong, size: c_int) { unsafe { let new_dst = dst as * mut c_void; let new_src = src as * const c_void; //println!("ptr_dst: {}", dst); //println!("ptr_src: {}", src); //println!("char src: {}",*(( src as usize + 8) as * const usize)); //let test = Test::new(234); //print!("test_ptr: "); //print_byte(& test as * const Test as * const u8); //print!("\n"); print_byte(new_dst as * const u8); print_byte(new_src as * const u8); //print_byte(new_src as * const u8); //println!("ptr: {}", dst); //println!("Data: {}", *(new_dst as * const i32)); ffi_mem(new_dst, new_src, size); } } #[unsafe(no_mangle)] extern "C" fn ffi_mem(dst: * mut c_void, src: * const c_void, size: ffi::c_int) { unsafe { memcpy(dst, src, size as usize); } } struct Test { test: i32 } impl Test { fn new(item: i32) -> Self { Self {test: item} } } #[cfg(test)] mod tests { use super::*; #[test] fn it_works() { unsafe { let t1 = Test::new(12); let t2 = Test::new(23434); let t1_ptr = &t1 as * const Test as * mut Test; let dst = t1_ptr as * mut c_void; let src = &t2 as * const Test; let src = src as * const c_void; ffi_mem(dst, src, size_of_val(& t2) as i32); assert_eq!(*(t1_ptr as * const i32), 23434); } //let result = add(2, 2); //assert_eq!(result, 4); } }