[PATCH 2/3] rust: split main into example, refactor to lib.rs
Harry van Haaren
harry.van.haaren at intel.com
Fri Apr 18 15:23:23 CEST 2025
Signed-off-by: Harry van Haaren <harry.van.haaren at intel.com>
---
rust_api_example/examples/eth_poll.rs | 35 +++++++++++++++++++
rust_api_example/src/{main.rs => lib.rs} | 43 ------------------------
2 files changed, 35 insertions(+), 43 deletions(-)
create mode 100644 rust_api_example/examples/eth_poll.rs
rename rust_api_example/src/{main.rs => lib.rs} (77%)
diff --git a/rust_api_example/examples/eth_poll.rs b/rust_api_example/examples/eth_poll.rs
new file mode 100644
index 0000000000..cde28df68d
--- /dev/null
+++ b/rust_api_example/examples/eth_poll.rs
@@ -0,0 +1,35 @@
+// Examples should not require any "unsafe" code.
+#![deny(unsafe_code)]
+
+use rust_api_example::dpdk::{self};
+
+fn main() {
+ let mut dpdk = dpdk::Eal::init().expect("dpdk must init ok");
+ let rx_mempool = dpdk::Mempool::new(4096);
+
+ let mut ports = dpdk.take_eth_ports().expect("take eth ports ok");
+ let mut p = ports.pop().unwrap();
+
+ p.rxqs(2, rx_mempool).expect("rxqs setup ok");
+ println!("{:?}", p);
+
+ let (mut rxqs, _txqs) = p.start();
+ println!("rxqs: {:?}", rxqs);
+
+ let rxq1 = rxqs.pop().unwrap();
+ let rxq2 = rxqs.pop().unwrap();
+
+ std::thread::spawn(move || {
+ let mut rxq = rxq1.enable_polling();
+ loop {
+ let _nb_mbufs = rxq.rx_burst(&mut [0; 32]);
+ std::thread::sleep(std::time::Duration::from_millis(1000));
+ }
+ });
+
+ let mut rxq = rxq2.enable_polling();
+ loop {
+ let _nb_mbufs = rxq.rx_burst(&mut [0; 32]);
+ std::thread::sleep(std::time::Duration::from_millis(1000));
+ }
+}
\ No newline at end of file
diff --git a/rust_api_example/src/main.rs b/rust_api_example/src/lib.rs
similarity index 77%
rename from rust_api_example/src/main.rs
rename to rust_api_example/src/lib.rs
index 8d0de50c30..0d13b06d85 100644
--- a/rust_api_example/src/main.rs
+++ b/rust_api_example/src/lib.rs
@@ -144,46 +144,3 @@ pub mod dpdk {
}
}
} // DPDK mod
-
-fn main() {
- let mut dpdk = dpdk::Eal::init().expect("dpdk must init ok");
- let rx_mempool = dpdk::Mempool::new(4096);
-
- let mut ports = dpdk.take_eth_ports().expect("take eth ports ok");
- let mut p = ports.pop().unwrap();
-
- p.rxqs(2, rx_mempool).expect("rxqs setup ok");
- println!("{:?}", p);
-
- let (mut rxqs, _txqs) = p.start();
- println!("rxqs: {:?}", rxqs);
-
- let rxq1 = rxqs.pop().unwrap();
- let rxq2 = rxqs.pop().unwrap();
-
- // spawn a new thread to use rxq1. This demonstrates that the RxqHandle
- // type can move between threads - it is not tied to the thread that created it.
- std::thread::spawn(move || {
- // Uncomment this: it fails to compile!
- // - Rxq2 would be used by this newly-spawned thread
- // -- specifically the variable was "moved" into this thread
- // - it is also used below (by the main thread)
- // "value used after move" is the error, on the below code
- // let mut rxq = rxq2.enable_polling();
-
- // see docs on enable_polling above to understand how the enable_polling()
- // function helps to achieve the thread-safety-at-compile-time goal.
- let mut rxq = rxq1.enable_polling();
- loop {
- let _nb_mbufs = rxq.rx_burst(&mut [0; 32]);
- std::thread::sleep(std::time::Duration::from_millis(1000));
- }
- });
-
- // main thread polling rxq2
- let mut rxq = rxq2.enable_polling();
- loop {
- let _nb_mbufs = rxq.rx_burst(&mut [0; 32]);
- std::thread::sleep(std::time::Duration::from_millis(1000));
- }
-}
--
2.34.1
More information about the dev
mailing list