Loading s3prg_rust_03_game...
#[no_mangle] fn say_hello() { println!("Hello from Rust!"); }
[lib] crate-type = ["cdylib"]
import sys import ctypes
print() native_lib_name=sys.argv[1] print('• loading', native_lib_name) native_lib=ctypes.CDLL(native_lib_name)
print() function_1_name='say_hello' print('• accessing', function_1_name) function_1=native_lib[function_1_name] print('• calling', function_1_name) function_1()
python mini_python_rust.py target/debug/libmini_python_rust.so
function_2.argtypes=[ctypes.c_double, ctypes.c_double] function_2.restype=ctypes.c_double
let operation = unsafe { std::ffi::CStr::from_ptr(c_operation) }.to_string_lossy();
let values = unsafe { std::slice::from_raw_parts_mut(data, len) };
rustup doc --std 'slice'.
array_type = ctypes.c_double * N
values = array_type(1.2, 2.3, 3.4 ... )
sudo apt update sudo apt install python3-tk
let content = ... les octets constitutifs du fichier ... ; let mut words = std::str::from_utf8(&content)? .lines() .map(|l| l.find('#').map_or(l, |pos| &l[0..pos])) .flat_map(|l| l.split_whitespace()) .filter(|w| !w.is_empty());
rustup doc --std 'std::iter::Iterator'.
word.parse::<usize>()?✍
word.parse::<u8>()?.
let p0 = Point { x: position.x.clamp(0, screen.width as i32), y: position.y.clamp(0, screen.height as i32), }; let p1 = Point { x: (position.x + image.width as i32).clamp(0, screen.width as i32), y: (position.y + image.height as i32).clamp(0, screen.height as i32), }; let dx = 0.max(p0.x - position.x); let dy = 0.max(p0.y - position.y); let mut i_idx = dy as usize * image.width + dx as usize; let mut s_idx = p0.y as usize * screen.width + p0.x as usize; let w = 0.max(p1.x - p0.x) as usize; for _ in p0.y..p1.y { let src = &image.pixels[i_idx..i_idx + w]; let dst = &mut screen.pixels[s_idx..s_idx + w];
// assign to each Color of `dst` the corresponding Color from `src`
i_idx += image.width; s_idx += screen.width; }
let msg = format!("motion {} {}\n", motion.x, motion.y);
fn read_lines_nonblocking( input: &mut BufReader<TcpStream> ) -> Result<Vec<String>, Box<dyn std::error::Error>> { fn inner( input: &mut BufReader<TcpStream> ) -> Result<Vec<String>, Box<dyn std::error::Error>> { let mut lines = Vec::new(); loop { let mut line = String::new(); match input.read_line(&mut line) { Ok(r) => { if !line.is_empty() { lines.push(line); } if r == 0 { lines.push(String::new()); // EOF break; } } Err(e) => { if e.kind() != ErrorKind::WouldBlock { Err(e)? } if line.is_empty() { // line not started, don't wait for the end break; } } } } Ok(lines) } input.get_mut().set_nonblocking(true)?; let result = inner(input); input.get_mut().set_nonblocking(false)?; result }
Result<(), Box<dyn std::error::Error>>car aucun résultat utile n'est attendu, mais il faudra tout de même pouvoir signaler les erreurs.
if let Some(data) = line.strip_prefix("position ") { let mut words = data.split_whitespace(); let x = words.next().ok_or("missing x")?.parse::<i32>()?; let y = words.next().ok_or("missing y")?.parse::<i32>()?; // ... }
if let Some(data) = request.strip_prefix("motion ") { let mut words = data.split_whitespace(); x += words.next().ok_or("missing x")?.parse::<i32>()?; y += words.next().ok_or("missing y")?.parse::<i32>()?; // ... }
let reply = format!("position {} {}\n", x, y);