Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Apply clippy suggestions #24

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub const MTU: usize = 1536;

/// Takes a bytes slice representing a UDP packet and returns the OSC packet as well as a slice of
/// any bytes remaining after the OSC packet.
pub fn decode_udp<'a>(msg: &'a [u8]) -> IResult<&'a [u8], OscPacket, OscError> {
pub fn decode_udp(msg: &[u8]) -> IResult<&[u8], OscPacket, OscError> {
decode_packet(msg, msg)
}

/// Takes a bytes slice from a TCP stream (or any stream-based protocol) and returns the first OSC
/// packet as well as a slice of the bytes remaining after the packet.
pub fn decode_tcp<'a>(msg: &'a [u8]) -> IResult<&'a [u8], Option<OscPacket>, OscError> {
pub fn decode_tcp(msg: &[u8]) -> IResult<&[u8], Option<OscPacket>, OscError> {
let (input, osc_packet_length) = be_u32(msg)?;

if osc_packet_length as usize > msg.len() {
Expand All @@ -35,15 +35,15 @@ pub fn decode_tcp<'a>(msg: &'a [u8]) -> IResult<&'a [u8], Option<OscPacket>, Osc

/// Takes a bytes slice from a TCP stream (or any stream-based protocol) and returns a vec of all
/// OSC packets in the slice as well as a slice of the bytes remaining after the last packet.
pub fn decode_tcp_vec<'a>(msg: &'a [u8]) -> IResult<&'a [u8], Vec<OscPacket>, OscError> {
pub fn decode_tcp_vec(msg: &[u8]) -> IResult<&[u8], Vec<OscPacket>, OscError> {
let mut input = msg;
let mut osc_packets = vec![];

while let (remainder, Some(osc_packet)) = decode_tcp(input)? {
input = remainder;
osc_packets.push(osc_packet);

if remainder.len() == 0 {
if remainder.is_empty() {
break;
}
};
Expand Down Expand Up @@ -125,7 +125,6 @@ fn read_bundle_element<'a>(input: &'a [u8], original_input: &'a[u8]) -> IResult<
},
|input| decode_packet(input, original_input),
)(input);
drop(elem_size);
result
}

Expand Down Expand Up @@ -233,7 +232,7 @@ fn read_blob<'a>(input: &'a[u8], original_input: &'a[u8]) -> IResult<&'a[u8], Os
)(input)
}

fn read_time_tag<'a>(input: &'a[u8]) -> IResult<&'a[u8], OscTime, OscError> {
fn read_time_tag(input: &[u8]) -> IResult<&[u8], OscTime, OscError> {
map(
tuple((be_u32, be_u32)),
|(seconds, fractional)| OscTime {
Expand All @@ -243,7 +242,7 @@ fn read_time_tag<'a>(input: &'a[u8]) -> IResult<&'a[u8], OscTime, OscError> {
)(input)
}

fn read_midi_message<'a>(input: &'a[u8]) -> IResult<&'a[u8], OscType, OscError> {
fn read_midi_message(input: &[u8]) -> IResult<&[u8], OscType, OscError> {
map(take(4usize), |buf: &[u8]| {
OscType::Midi(OscMidiMessage {
port: buf[0],
Expand All @@ -254,7 +253,7 @@ fn read_midi_message<'a>(input: &'a[u8]) -> IResult<&'a[u8], OscType, OscError>
})(input)
}

fn read_osc_color<'a>(input: &'a[u8]) -> IResult<&'a[u8], OscType, OscError> {
fn read_osc_color(input: &[u8]) -> IResult<&[u8], OscType, OscError> {
map(take(4usize), |buf: &[u8]| {
OscType::Color(OscColor {
red: buf[0],
Expand Down
9 changes: 4 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,19 @@ mod tests {
fn osc_times_can_be_converted_to_and_from_system_times() {
let mut times = vec![];
// Sweep across a few numbers to check for tolerance
for seconds in vec![
for seconds in &[
// We don't start at zero because times before the UNIX_EPOCH cannot be converted to
// OscTime.
OscTime::UNIX_OFFSET as u32,
OscTime::UNIX_OFFSET as u32 + 1,
OscTime::UNIX_OFFSET as u32 + 2,
OscTime::UNIX_OFFSET as u32 + 3,
u32::MAX - 1,
u32::MAX,
] {
u32::MAX] {
let fractional_max = 100;
for fractional in 0..fractional_max {
times.push((seconds, fractional));
times.push((seconds, fractional_max - fractional));
times.push((*seconds, fractional));
times.push((*seconds, fractional_max - fractional));
}
}

Expand Down
8 changes: 3 additions & 5 deletions tests/address_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ use rosc::address::Matcher;
fn test_matcher() {
let matcher = Matcher::new("/oscillator/[0-9]/*/pre[!1234?*]post/{frequency,phase}/x?")
.expect("Matcher::new");
assert_eq!(
assert!(
matcher
.match_address("/oscillator/1/something/preXpost/phase/xy")
.expect("should match"),
true
);
assert_eq!(
matcher
assert!(
!matcher
.match_address("/oscillator/1/something/pre1post/phase/xy")
.expect("should not match"),
false
);
matcher.match_address("invalid_address").expect_err("should fail because address does not start with a slash");
}
Expand Down
14 changes: 7 additions & 7 deletions tests/decoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate byteorder;
extern crate rosc;

use byteorder::{BigEndian, ByteOrder};
use std::mem;


use rosc::{decoder, encoder, OscBundle, OscPacket, OscTime, OscType};

Expand Down Expand Up @@ -75,6 +75,7 @@ fn test_decode_udp_empty_bundle() {
}

#[test]
#[allow(clippy::approx_constant)]
fn test_decode_udp_args() {
// /another/valid/address/123 ,fdih 3.1415 3.14159265359 12345678i32
// -1234567891011
Expand All @@ -91,12 +92,12 @@ fn test_decode_udp_args() {
assert_eq!(BigEndian::read_f64(&d_bytes), d);

let i = 12345678i32;
let i_bytes: [u8; 4] = unsafe { mem::transmute(i.to_be()) };
let i_bytes: [u8; 4] = i.to_be().to_ne_bytes();

let l = -1234567891011i64;
let h_bytes: [u8; 8] = unsafe { mem::transmute(l.to_be()) };
let h_bytes: [u8; 8] = l.to_be().to_ne_bytes();

let blob_size: [u8; 4] = unsafe { mem::transmute(6u32.to_be()) };
let blob_size: [u8; 4] = 6u32.to_be().to_ne_bytes();
let blob: Vec<u8> = vec![1u8, 2u8, 3u8, 4u8, 5u8, 6u8];

let s = "I am an osc test string.";
Expand All @@ -105,7 +106,7 @@ fn test_decode_udp_args() {
let s_bytes: Vec<u8> = encoder::encode_string(s);

let c = '$';
let c_bytes: [u8; 4] = unsafe { mem::transmute((c as u32).to_be()) };
let c_bytes: [u8; 4] = (c as u32).to_be().to_ne_bytes();

let a = vec![OscType::Int(i), OscType::Float(f), OscType::Int(i)];

Expand All @@ -124,8 +125,7 @@ fn test_decode_udp_args() {
// array content
.chain(i_bytes.iter())
.chain(f_bytes.iter())
.chain(i_bytes.iter())
.map(|x| *x)
.chain(i_bytes.iter()).copied()
.collect::<Vec<u8>>();

let merged: Vec<u8> = addr
Expand Down
1 change: 1 addition & 0 deletions tests/encoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn test_encode_empty_bundle() {
}

#[test]
#[allow(clippy::approx_constant)]
fn test_encode_message_with_args() {
let msg_packet = OscPacket::Message(OscMessage {
addr: "/another/address/1".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion tests/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rosc::{OscArray, OscType};
#[test]
fn test_osc_array_from_iter() {
use std::iter::FromIterator;
let iter = (0..3).map(|i| OscType::Int(i));
let iter = (0..3).map(OscType::Int);
let osc_arr = OscArray::from_iter(iter);
assert_eq!(
osc_arr,
Expand Down