Skip to content

Commit

Permalink
add From<Response<&[u8]>> for Response<Vec<u8>>, cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
omelia-iliffe committed Dec 12, 2024
1 parent 82977b1 commit 6edaa7d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
22 changes: 14 additions & 8 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use core::time::Duration;
use crate::endian::{read_u16_le, read_u32_le, read_u8_le};
use crate::serial_port::SerialPort;
use crate::{ReadError, TransferError, WriteError};
use core::time::Duration;

#[cfg(feature = "serial2")]
use std::path::Path;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, vec::Vec};
use crate::instructions::instruction_id;
use crate::messaging::Messenger;
use crate::packet::{Packet, STATUS_HEADER_SIZE};
#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, vec::Vec};

/// Dynamixel Protocol 2 communication bus.
pub struct Bus<ReadBuffer, WriteBuffer, T: SerialPort> {
Expand Down Expand Up @@ -85,11 +85,7 @@ where
///
/// The serial port must already be configured in raw mode with the correct baud rate,
/// character size (8), parity (disabled) and stop bits (1).
pub fn with_buffers(
serial_port: T,
read_buffer: ReadBuffer,
write_buffer: WriteBuffer,
) -> Result<Self, T::Error> {
pub fn with_buffers(serial_port: T, read_buffer: ReadBuffer, write_buffer: WriteBuffer) -> Result<Self, T::Error> {
let messenger = Messenger::with_buffers(serial_port, read_buffer, write_buffer)?;
Ok(Self { messenger })
}
Expand Down Expand Up @@ -318,6 +314,16 @@ impl<'a> TryFrom<StatusPacket<'a>> for Response<u32> {
}
}

impl From<Response<&[u8]>> for Response<Vec<u8>> {
fn from(response: Response<&[u8]>) -> Self {
Response {
motor_id: response.motor_id,
alert: response.alert,
data: response.data.into(),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
18 changes: 7 additions & 11 deletions src/instructions/bulk_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::endian::{write_u16_le, write_u8_le};
use crate::serial_port::SerialPort;
use crate::{Bus, ReadError, Response, WriteError};

#[cfg(feature = "alloc")]
use alloc::{vec::Vec, borrow::ToOwned};
use crate::packet::Packet;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

impl<ReadBuffer, WriteBuffer, T> Bus<ReadBuffer, WriteBuffer, T>
where
Expand Down Expand Up @@ -79,20 +79,16 @@ where
/// The protocol forbids specifying the same motor ID multiple times.
/// This function panics if the same motor ID is used for more than one read.
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn bulk_read<Read>(&mut self, reads: &[Read]) -> Result<Vec<Result<Response<Vec<u8>>, ReadError<T::Error>>>, crate::TransferError<T::Error>>
pub fn bulk_read<Read>(
&mut self,
reads: &[Read],
) -> Result<Vec<Result<Response<Vec<u8>>, ReadError<T::Error>>>, crate::TransferError<T::Error>>

Check warning on line 85 in src/instructions/bulk_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions

Check warning on line 85 in src/instructions/bulk_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
where
Read: AsRef<BulkReadData>,
{
let mut responses = Vec::with_capacity(reads.len());

self.bulk_read_cb(reads, |_read, response| match response {
Err(e) => responses.push(Err(e)),
Ok(response) => responses.push(Ok(Response {
motor_id: response.motor_id,
alert: response.alert,
data: response.data.to_owned(),
})),
})?;
self.bulk_read_cb(reads, |_read, response| responses.push(response.map(Into::into)))?;
Ok(responses)
}
}
29 changes: 17 additions & 12 deletions src/instructions/sync_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{Bus, ReadError, Response, WriteError};

use crate::packet::Packet;
#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, vec::Vec};
use alloc::vec::Vec;

impl<ReadBuffer, WriteBuffer, T> Bus<ReadBuffer, WriteBuffer, T>
where
Expand Down Expand Up @@ -132,14 +132,7 @@ where
count: u16,
) -> Result<Vec<Result<Response<Vec<u8>>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {

Check warning on line 133 in src/instructions/sync_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
let mut result = Vec::with_capacity(motor_ids.len());
self.sync_read_cb(motor_ids, address, count, |data| match data {
Err(e) => result.push(Err(e)),
Ok(response) => result.push(Ok(Response {
motor_id: response.motor_id,
alert: response.alert,
data: response.data.to_owned(),
})),
})?;
self.sync_read_cb(motor_ids, address, count, |data| result.push(data.map(Into::into)))?;
Ok(result)
}

Expand All @@ -148,7 +141,11 @@ where
/// If this function fails to get the data from any of the motors, the entire function retrns an error.
/// If you need access to the data from other motors, or if you want acces to the error for each motor, see [`Self::sync_read_u8_cb`].
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn sync_read_u8<'a>(&'a mut self, motor_ids: &'a [u8], address: u16) -> Result<Vec<Result<Response<u8>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {
pub fn sync_read_u8<'a>(
&'a mut self,
motor_ids: &'a [u8],
address: u16,
) -> Result<Vec<Result<Response<u8>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {

Check warning on line 148 in src/instructions/sync_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
let mut result = Vec::with_capacity(motor_ids.len());
self.sync_read_u8_cb(motor_ids, address, |data| result.push(data))?;
Ok(result)
Expand All @@ -159,7 +156,11 @@ where
/// If this function fails to get the data from any of the motors, the entire function retrns an error.
/// If you need access to the data from other motors, or if you want acces to the error for each motor, see [`Self::sync_read_u16_cb`].
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn sync_read_u16<'a>(&'a mut self, motor_ids: &'a [u8], address: u16) -> Result<Vec<Result<Response<u16>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {
pub fn sync_read_u16<'a>(
&'a mut self,
motor_ids: &'a [u8],
address: u16,
) -> Result<Vec<Result<Response<u16>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {

Check warning on line 163 in src/instructions/sync_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
let mut result = Vec::with_capacity(motor_ids.len());
self.sync_read_u16_cb(motor_ids, address, |data| result.push(data))?;
Ok(result)
Expand All @@ -170,7 +171,11 @@ where
/// If this function fails to get the data from any of the motors, the entire function retrns an error.
/// If you need access to the data from other motors, or if you want acces to the error for each motor, see [`Self::sync_read_u32_cb`].
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn sync_read_u32<'a>(&'a mut self, motor_ids: &'a [u8], address: u16) -> Result<Vec<Result<Response<u32>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {
pub fn sync_read_u32<'a>(
&'a mut self,
motor_ids: &'a [u8],
address: u16,
) -> Result<Vec<Result<Response<u32>, ReadError<T::Error>>>, crate::TransferError<T::Error>> {

Check warning on line 178 in src/instructions/sync_read.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
let mut result = Vec::with_capacity(motor_ids.len());
self.sync_read_u32_cb(motor_ids, address, |data| result.push(data))?;
Ok(result)
Expand Down

0 comments on commit 6edaa7d

Please sign in to comment.