Skip to content

Commit e479b76

Browse files
committed
Rust API: Add Transport::from_device() for single-device construction
1 parent 50adada commit e479b76

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/rust/moteus/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ let cmd = PositionCommand::new()
6868
For more control, you can specify a transport explicitly:
6969

7070
```rust,no_run
71-
use moteus::{BlockingController, TransportOptions};
71+
use moteus::{BlockingController, Transport};
72+
use moteus::transport::socketcan::SocketCan;
7273
7374
fn main() -> Result<(), moteus::Error> {
74-
let opts = TransportOptions::new()
75-
.socketcan_interfaces(vec!["can0"]);
76-
let mut ctrl = BlockingController::with_options(1, &opts);
75+
let transport = Transport::from_device(SocketCan::new("can0")?);
76+
let mut ctrl = BlockingController::new(1)
77+
.transport(transport);
7778
7879
ctrl.set_stop()?;
7980
Ok(())

lib/rust/moteus/src/transport/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,26 @@ fn compute_parent_indices(devices: &[Box<dyn TransportDevice>]) -> Vec<usize> {
476476
}
477477

478478
impl Transport {
479+
/// Creates a transport from a single device.
480+
///
481+
/// This is the simplest way to create a transport when you know
482+
/// exactly which device to use.
483+
///
484+
/// # Example
485+
///
486+
/// ```no_run
487+
/// use moteus::transport::socketcan::SocketCan;
488+
/// use moteus::Transport;
489+
///
490+
/// fn main() -> Result<(), moteus::Error> {
491+
/// let transport = Transport::from_device(SocketCan::new("can0")?);
492+
/// Ok(())
493+
/// }
494+
/// ```
495+
pub fn from_device(device: impl TransportDevice + 'static) -> Self {
496+
Self::new(vec![Box::new(device)])
497+
}
498+
479499
/// Creates a new transport from a list of devices.
480500
///
481501
/// The devices will be assigned sequential IDs starting from 0.

0 commit comments

Comments
 (0)