From 35810c9f875a7d9b3bebe24f71adfa83a7288010 Mon Sep 17 00:00:00 2001 From: Lofty Date: Fri, 10 Oct 2025 15:04:19 +0100 Subject: [PATCH] Rust cleanup (#1573) * rust: formatting cleanup * rust: explicitly mark as ISC license * rust: use std::ffi C types instead of libc dependency --- rust/example_printnets/src/lib.rs | 4 +- rust/nextpnr/Cargo.toml | 2 +- rust/nextpnr/src/lib.rs | 70 +++++++++++++++++-------------- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/rust/example_printnets/src/lib.rs b/rust/example_printnets/src/lib.rs index 4c62c385..23dbc3bc 100644 --- a/rust/example_printnets/src/lib.rs +++ b/rust/example_printnets/src/lib.rs @@ -8,7 +8,9 @@ pub extern "C" fn rust_example_printnets(ctx: &mut Context) { let driver = net.driver(); println!(" {}:", ctx.name_of(name).to_str().unwrap()); if let Some(driver) = driver { - let cell = driver.cell().map_or("(no cell)", |cell| ctx.name_of(cell.name()).to_str().unwrap()); + let cell = driver.cell().map_or("(no cell)", |cell| { + ctx.name_of(cell.name()).to_str().unwrap() + }); let port = ctx.name_of(driver.port()).to_str().unwrap(); println!(" driver: {cell}.{port}"); } else { diff --git a/rust/nextpnr/Cargo.toml b/rust/nextpnr/Cargo.toml index 8052d132..d454903c 100644 --- a/rust/nextpnr/Cargo.toml +++ b/rust/nextpnr/Cargo.toml @@ -2,6 +2,7 @@ name = "nextpnr" version = "0.1.0" edition = "2024" +license = "ISC" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -11,4 +12,3 @@ path = "src/lib.rs" crate-type = ["rlib"] [dependencies] -libc = "0.2" diff --git a/rust/nextpnr/src/lib.rs b/rust/nextpnr/src/lib.rs index e8cbe5c7..21524110 100644 --- a/rust/nextpnr/src/lib.rs +++ b/rust/nextpnr/src/lib.rs @@ -1,6 +1,4 @@ -use std::{ffi::CStr, marker::PhantomData, sync::Mutex}; - -use libc::c_char; +use std::{ffi::{c_char, c_int, CStr, CString}, marker::PhantomData, sync::Mutex}; #[derive(Clone, Copy)] #[repr(C)] @@ -53,9 +51,9 @@ impl NetInfo { #[must_use] pub fn users(&self) -> NetUserIter<'_> { - NetUserIter { + NetUserIter { iter: unsafe { npnr_context_net_user_iter(self) }, - phantom_data: PhantomData + phantom_data: PhantomData, } } } @@ -91,7 +89,7 @@ impl PortRef { #[derive(Clone, Copy, PartialEq, Eq, Hash)] #[repr(transparent)] -pub struct IdString(libc::c_int); +pub struct IdString(c_int); /// A type representing a bel name. #[derive(Clone, Copy, PartialEq, Eq)] @@ -163,9 +161,9 @@ impl WireId { #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Loc { - pub x: libc::c_int, - pub y: libc::c_int, - pub z: libc::c_int, + pub x: c_int, + pub y: c_int, + pub z: c_int, } impl From<(i32, i32)> for Loc { @@ -297,19 +295,28 @@ impl Context { #[must_use] pub fn bels(&self) -> BelIter<'_> { let iter = unsafe { npnr_context_get_bels(self) }; - BelIter { iter, phantom_data: PhantomData } + BelIter { + iter, + phantom_data: PhantomData, + } } #[must_use] pub fn pips(&self) -> PipIter<'_> { let iter = unsafe { npnr_context_get_pips(self) }; - PipIter { iter, phantom_data: PhantomData } + PipIter { + iter, + phantom_data: PhantomData, + } } #[must_use] pub fn wires(&self) -> WireIter<'_> { let iter = unsafe { npnr_context_get_wires(self) }; - WireIter { iter, phantom_data: PhantomData } + WireIter { + iter, + phantom_data: PhantomData, + } } #[must_use] @@ -337,8 +344,8 @@ impl Context { #[must_use] pub fn pip_direction(&self, pip: PipId) -> Loc { - let mut src = Loc{x: 0, y: 0, z: 0}; - let mut dst = Loc{x: 0, y: 0, z: 0}; + let mut src = Loc { x: 0, y: 0, z: 0 }; + let mut dst = Loc { x: 0, y: 0, z: 0 }; let mut pips = 0; for pip in self.get_uphill_pips(self.pip_src_wire(pip)) { @@ -364,7 +371,11 @@ impl Context { dst.y /= pips; } - Loc{x: dst.x - src.x, y: dst.y - src.y, z: 0} + Loc { + x: dst.x - src.x, + y: dst.y - src.y, + z: 0, + } } #[must_use] @@ -383,7 +394,7 @@ impl Context { #[must_use] pub fn id(&self, s: &str) -> IdString { - let s = std::ffi::CString::new(s).unwrap(); + let s = CString::new(s).unwrap(); unsafe { npnr_context_id(self, s.as_ptr()) } } @@ -424,8 +435,8 @@ unsafe extern "C" { safe fn npnr_wireid_null() -> WireId; safe fn npnr_pipid_null() -> PipId; - fn npnr_context_get_grid_dim_x(ctx: &Context) -> libc::c_int; - fn npnr_context_get_grid_dim_y(ctx: &Context) -> libc::c_int; + fn npnr_context_get_grid_dim_x(ctx: &Context) -> c_int; + fn npnr_context_get_grid_dim_y(ctx: &Context) -> c_int; fn npnr_context_bind_bel( ctx: &mut Context, bel: BelId, @@ -455,18 +466,14 @@ unsafe extern "C" { fn npnr_context_get_pip_delay(ctx: &Context, pip: PipId) -> f32; fn npnr_context_get_wire_delay(ctx: &Context, wire: WireId) -> f32; fn npnr_context_get_pip_location(ctx: &Context, pip: PipId) -> Loc; - fn npnr_context_check_pip_avail_for_net( - ctx: &Context, - pip: PipId, - net: &NetInfo, - ) -> bool; + fn npnr_context_check_pip_avail_for_net(ctx: &Context, pip: PipId, net: &NetInfo) -> bool; fn npnr_context_check(ctx: &Context); fn npnr_context_debug(ctx: &Context) -> bool; fn npnr_context_id(ctx: &Context, s: *const c_char) -> IdString; - fn npnr_context_name_of(ctx: &Context, s: IdString) -> *const libc::c_char; - fn npnr_context_name_of_pip(ctx: &Context, pip: PipId) -> *const libc::c_char; - fn npnr_context_name_of_wire(ctx: &Context, wire: WireId) -> *const libc::c_char; + fn npnr_context_name_of(ctx: &Context, s: IdString) -> *const c_char; + fn npnr_context_name_of_pip(ctx: &Context, pip: PipId) -> *const c_char; + fn npnr_context_name_of_wire(ctx: &Context, wire: WireId) -> *const c_char; fn npnr_context_verbose(ctx: &Context) -> bool; fn npnr_context_get_netinfo_source_wire(ctx: &Context, net: &NetInfo) -> WireId; @@ -483,9 +490,9 @@ unsafe extern "C" { fn npnr_netinfo_udata_set(net: &mut NetInfo, value: NetIndex); fn npnr_portref_cell(port: &PortRef) -> Option<&CellInfo>; - fn npnr_portref_port(port: &PortRef) -> libc::c_int; + fn npnr_portref_port(port: &PortRef) -> c_int; fn npnr_cellinfo_get_location(cell: &CellInfo) -> Loc; - fn npnr_cellinfo_name(cell: &CellInfo) -> libc::c_int; + fn npnr_cellinfo_name(cell: &CellInfo) -> c_int; fn npnr_context_get_pips_downhill(ctx: &Context, wire: WireId) -> &mut RawDownhillIter; fn npnr_delete_downhill_iter(iter: &mut RawDownhillIter); @@ -520,14 +527,14 @@ unsafe extern "C" { fn npnr_context_net_iter(ctx: &Context) -> &mut RawNetIter; fn npnr_delete_net_iter(iter: &mut RawNetIter); fn npnr_inc_net_iter(iter: &mut RawNetIter); - fn npnr_deref_net_iter_first(iter: &RawNetIter) -> libc::c_int; + fn npnr_deref_net_iter_first(iter: &RawNetIter) -> c_int; fn npnr_deref_net_iter_second(iter: &RawNetIter) -> &mut NetInfo; fn npnr_is_net_iter_done(iter: &RawNetIter) -> bool; fn npnr_context_cell_iter(ctx: &Context) -> &mut RawCellIter; fn npnr_delete_cell_iter(iter: &mut RawCellIter); fn npnr_inc_cell_iter(iter: &mut RawCellIter); - fn npnr_deref_cell_iter_first(iter: &mut RawCellIter) -> libc::c_int; + fn npnr_deref_cell_iter_first(iter: &mut RawCellIter) -> c_int; fn npnr_deref_cell_iter_second(iter: &mut RawCellIter) -> &mut CellInfo; fn npnr_is_cell_iter_done(iter: &mut RawCellIter) -> bool; @@ -535,7 +542,7 @@ unsafe extern "C" { fn npnr_delete_net_user_iter(iter: &mut RawNetUserIter); fn npnr_inc_net_user_iter(iter: &mut RawNetUserIter); fn npnr_deref_net_user_iter_cell(iter: &mut RawNetUserIter) -> &mut CellInfo; - fn npnr_deref_net_user_iter_port(iter: &mut RawNetUserIter) -> libc::c_int; + fn npnr_deref_net_user_iter_port(iter: &mut RawNetUserIter) -> c_int; fn npnr_is_net_user_iter_done(iter: &mut RawNetUserIter) -> bool; } @@ -851,4 +858,3 @@ macro_rules! log_error { unsafe { crate::npnr::npnr_log_error(s.as_ptr()); } }; } -