diff --git a/benches/block.rs b/benches/block.rs index ebd35e58f..9c049cd36 100644 --- a/benches/block.rs +++ b/benches/block.rs @@ -5,7 +5,7 @@ use valence::block::{BlockKind, BlockState, PropName, PropValue}; use valence::ItemKind; #[divan::bench] -pub fn from_kind(bencher: Bencher) { +pub(crate) fn from_kind(bencher: Bencher) { bencher.bench(|| { for kind in black_box(BlockKind::ALL) { black_box(BlockState::from_kind(kind)); @@ -13,7 +13,7 @@ pub fn from_kind(bencher: Bencher) { }); } #[divan::bench] -pub fn to_kind(bencher: Bencher) { +pub(crate) fn to_kind(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -22,7 +22,7 @@ pub fn to_kind(bencher: Bencher) { }); } #[divan::bench] -pub fn get_prop(bencher: Bencher) { +pub(crate) fn get_prop(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -31,7 +31,7 @@ pub fn get_prop(bencher: Bencher) { }); } #[divan::bench] -pub fn set_prop(bencher: Bencher) { +pub(crate) fn set_prop(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -40,7 +40,7 @@ pub fn set_prop(bencher: Bencher) { }); } #[divan::bench] -pub fn is_liquid(bencher: Bencher) { +pub(crate) fn is_liquid(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -49,7 +49,7 @@ pub fn is_liquid(bencher: Bencher) { }); } #[divan::bench] -pub fn is_opaque(bencher: Bencher) { +pub(crate) fn is_opaque(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -58,7 +58,7 @@ pub fn is_opaque(bencher: Bencher) { }); } #[divan::bench] -pub fn is_replaceable(bencher: Bencher) { +pub(crate) fn is_replaceable(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -67,7 +67,7 @@ pub fn is_replaceable(bencher: Bencher) { }); } #[divan::bench] -pub fn luminance(bencher: Bencher) { +pub(crate) fn luminance(bencher: Bencher) { let states = BlockKind::ALL.map(BlockKind::to_state); bencher.bench(|| { for state in black_box(states) { @@ -76,7 +76,7 @@ pub fn luminance(bencher: Bencher) { }); } #[divan::bench] -pub fn to_item_kind(bencher: Bencher) { +pub(crate) fn to_item_kind(bencher: Bencher) { bencher.bench(|| { for kind in black_box(BlockKind::ALL) { black_box(kind.to_item_kind()); @@ -84,7 +84,7 @@ pub fn to_item_kind(bencher: Bencher) { }); } #[divan::bench] -pub fn from_item_kind(bencher: Bencher) { +pub(crate) fn from_item_kind(bencher: Bencher) { bencher.bench(|| { for kind in black_box(ItemKind::ALL) { black_box(BlockKind::from_item_kind(kind)); diff --git a/benches/decode_array.rs b/benches/decode_array.rs index 028c82030..bd9be59f9 100644 --- a/benches/decode_array.rs +++ b/benches/decode_array.rs @@ -4,7 +4,7 @@ use divan::Bencher; use valence::protocol::{Decode, Encode}; #[divan::bench] -pub fn decode_small_array(bencher: Bencher) { +pub(crate) fn decode_small_array(bencher: Bencher) { let floats = [123.0, 456.0, 789.0]; let mut buf = [0_u8; 24]; @@ -17,7 +17,7 @@ pub fn decode_small_array(bencher: Bencher) { } #[divan::bench] -pub fn decode_large_array(bencher: Bencher) { +pub(crate) fn decode_large_array(bencher: Bencher) { let bytes = [42; 4096]; bencher.bench(|| { diff --git a/benches/idle.rs b/benches/idle.rs index 3aa8e6a70..a563b832e 100644 --- a/benches/idle.rs +++ b/benches/idle.rs @@ -4,7 +4,7 @@ use valence::prelude::*; /// Benches the performance of a single server tick while nothing much is /// happening. #[divan::bench] -pub fn idle_update(bencher: Bencher) { +pub(crate) fn idle_update(bencher: Bencher) { let mut app = App::new(); app.add_plugins(DefaultPlugins); diff --git a/benches/packet.rs b/benches/packet.rs index 560dd6e1f..c3b741ab9 100644 --- a/benches/packet.rs +++ b/benches/packet.rs @@ -12,7 +12,7 @@ use valence::text::IntoText; use valence_server::protocol::Velocity; use valence_server::CompressionThreshold; -pub fn setup<'a>() -> ( +pub(crate) fn setup<'a>() -> ( PacketEncoder, ChunkDataS2c<'a>, PlayerListHeaderS2c<'a>, diff --git a/crates/valence_command/src/parsers.rs b/crates/valence_command/src/parsers.rs index 1b1dc41e8..78a810d61 100644 --- a/crates/valence_command/src/parsers.rs +++ b/crates/valence_command/src/parsers.rs @@ -175,28 +175,6 @@ impl<'a> ParseInput<'a> { } } -#[test] -fn test_parse_input() { - let mut input = ParseInput::new("The QuIck brown FOX jumps over the lazy dog"); - assert_eq!(input.peek(), Some('T')); - assert_eq!(input.peek_n(0), ""); - assert_eq!(input.peek_n(1), "T"); - assert_eq!(input.peek_n(2), "Th"); - assert_eq!(input.peek_n(3), "The"); - - assert_eq!(input.peek_word(), "The"); - input.pop_word(); - input.skip_whitespace(); - assert_eq!(input.peek_word(), "QuIck"); - - assert!(input.match_next("quick")); - input.pop(); - assert_eq!(input.peek_word(), "brown"); - - assert!(input.match_next("brown fox")); - assert_eq!(input.pop_all(), Some(" jumps over the lazy dog")); -} - #[derive(Debug, Error)] pub enum CommandArgParseError { // these should be player facing and not disclose internal information @@ -249,58 +227,83 @@ where } } -#[test] -fn test_absolute_or_relative() { - let mut input = ParseInput::new("~"); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Relative(0) - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("~1"); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Relative(1) - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("~1.5"); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Relative(1.5) - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("1"); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Absolute(1) - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("1.5 "); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Absolute(1.5) - ); - assert!(!input.is_done()); - - let mut input = ParseInput::new("1.5 2"); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Absolute(1.5) - ); - assert!(!input.is_done()); - assert_eq!( - AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), - AbsoluteOrRelative::Absolute(2.0) - ); - assert!(input.is_done()); -} - impl Default for AbsoluteOrRelative { fn default() -> Self { AbsoluteOrRelative::Absolute(T::default()) } } + +#[cfg(test)] +mod test { + use super::*; + #[test] + fn test_parse_input() { + let mut input = ParseInput::new("The QuIck brown FOX jumps over the lazy dog"); + assert_eq!(input.peek(), Some('T')); + assert_eq!(input.peek_n(0), ""); + assert_eq!(input.peek_n(1), "T"); + assert_eq!(input.peek_n(2), "Th"); + assert_eq!(input.peek_n(3), "The"); + + assert_eq!(input.peek_word(), "The"); + input.pop_word(); + input.skip_whitespace(); + assert_eq!(input.peek_word(), "QuIck"); + + assert!(input.match_next("quick")); + input.pop(); + assert_eq!(input.peek_word(), "brown"); + + assert!(input.match_next("brown fox")); + assert_eq!(input.pop_all(), Some(" jumps over the lazy dog")); + } + #[test] + fn test_absolute_or_relative() { + let mut input = ParseInput::new("~"); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Relative(0) + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("~1"); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Relative(1) + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("~1.5"); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Relative(1.5) + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("1"); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Absolute(1) + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("1.5 "); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Absolute(1.5) + ); + assert!(!input.is_done()); + + let mut input = ParseInput::new("1.5 2"); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Absolute(1.5) + ); + assert!(!input.is_done()); + assert_eq!( + AbsoluteOrRelative::::parse_arg(&mut input).unwrap(), + AbsoluteOrRelative::Absolute(2.0) + ); + assert!(input.is_done()); + } +} diff --git a/crates/valence_command/src/parsers/block_pos.rs b/crates/valence_command/src/parsers/block_pos.rs index 700b443a1..93480fe96 100644 --- a/crates/valence_command/src/parsers/block_pos.rs +++ b/crates/valence_command/src/parsers/block_pos.rs @@ -25,38 +25,43 @@ impl CommandArg for BlockPos { } } -#[test] -fn test_block_pos() { - let mut input = ParseInput::new("~-1 2 3"); - assert_eq!( - BlockPos::parse_arg(&mut input).unwrap(), - BlockPos { - x: AbsoluteOrRelative::Relative(-1), - y: AbsoluteOrRelative::Absolute(2), - z: AbsoluteOrRelative::Absolute(3) - } - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("-1 ~2 3 "); - assert_eq!( - BlockPos::parse_arg(&mut input).unwrap(), - BlockPos { - x: AbsoluteOrRelative::Absolute(-1), - y: AbsoluteOrRelative::Relative(2), - z: AbsoluteOrRelative::Absolute(3) - } - ); - assert!(!input.is_done()); - - let mut input = ParseInput::new("-1 2 ~3 4"); - assert_eq!( - BlockPos::parse_arg(&mut input).unwrap(), - BlockPos { - x: AbsoluteOrRelative::Absolute(-1), - y: AbsoluteOrRelative::Absolute(2), - z: AbsoluteOrRelative::Relative(3) - } - ); - assert!(!input.is_done()); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_block_pos() { + let mut input = ParseInput::new("~-1 2 3"); + assert_eq!( + BlockPos::parse_arg(&mut input).unwrap(), + BlockPos { + x: AbsoluteOrRelative::Relative(-1), + y: AbsoluteOrRelative::Absolute(2), + z: AbsoluteOrRelative::Absolute(3) + } + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("-1 ~2 3 "); + assert_eq!( + BlockPos::parse_arg(&mut input).unwrap(), + BlockPos { + x: AbsoluteOrRelative::Absolute(-1), + y: AbsoluteOrRelative::Relative(2), + z: AbsoluteOrRelative::Absolute(3) + } + ); + assert!(!input.is_done()); + + let mut input = ParseInput::new("-1 2 ~3 4"); + assert_eq!( + BlockPos::parse_arg(&mut input).unwrap(), + BlockPos { + x: AbsoluteOrRelative::Absolute(-1), + y: AbsoluteOrRelative::Absolute(2), + z: AbsoluteOrRelative::Relative(3) + } + ); + assert!(!input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/bool.rs b/crates/valence_command/src/parsers/bool.rs index baef5e454..45996631e 100644 --- a/crates/valence_command/src/parsers/bool.rs +++ b/crates/valence_command/src/parsers/bool.rs @@ -21,22 +21,27 @@ impl CommandArg for bool { } } -#[test] -fn test_bool() { - let mut input = ParseInput::new("true"); - assert!(bool::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("false"); - assert!(!bool::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); + #[test] + fn test_bool() { + let mut input = ParseInput::new("true"); + assert!(bool::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); - let mut input = ParseInput::new("false "); - assert!(!bool::parse_arg(&mut input).unwrap()); - assert!(!input.is_done()); + let mut input = ParseInput::new("false"); + assert!(!bool::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); - let mut input = ParseInput::new("falSe trUe"); - assert!(!bool::parse_arg(&mut input).unwrap()); - assert!(bool::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); + let mut input = ParseInput::new("false "); + assert!(!bool::parse_arg(&mut input).unwrap()); + assert!(!input.is_done()); + + let mut input = ParseInput::new("falSe trUe"); + assert!(!bool::parse_arg(&mut input).unwrap()); + assert!(bool::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/column_pos.rs b/crates/valence_command/src/parsers/column_pos.rs index 041c3e3dd..795b71dc3 100644 --- a/crates/valence_command/src/parsers/column_pos.rs +++ b/crates/valence_command/src/parsers/column_pos.rs @@ -25,40 +25,45 @@ impl CommandArg for ColumnPos { } } -#[test] -fn test_column_pos() { - let mut input = ParseInput::new("~-1 2 3"); - assert_eq!( - ColumnPos::parse_arg(&mut input).unwrap(), - ColumnPos { - x: AbsoluteOrRelative::Relative(-1), - y: AbsoluteOrRelative::Absolute(2), - z: AbsoluteOrRelative::Absolute(3) - } - ); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("-1 ~2 3 hello"); - assert_eq!( - ColumnPos::parse_arg(&mut input).unwrap(), - ColumnPos { - x: AbsoluteOrRelative::Absolute(-1), - y: AbsoluteOrRelative::Relative(2), - z: AbsoluteOrRelative::Absolute(3) - } - ); - assert!(!input.is_done()); - input.skip_whitespace(); - assert!(input.match_next("hello")); + #[test] + fn test_column_pos() { + let mut input = ParseInput::new("~-1 2 3"); + assert_eq!( + ColumnPos::parse_arg(&mut input).unwrap(), + ColumnPos { + x: AbsoluteOrRelative::Relative(-1), + y: AbsoluteOrRelative::Absolute(2), + z: AbsoluteOrRelative::Absolute(3) + } + ); + assert!(input.is_done()); - let mut input = ParseInput::new("-1 2 ~3 4"); - assert_eq!( - ColumnPos::parse_arg(&mut input).unwrap(), - ColumnPos { - x: AbsoluteOrRelative::Absolute(-1), - y: AbsoluteOrRelative::Absolute(2), - z: AbsoluteOrRelative::Relative(3) - } - ); - assert!(!input.is_done()); + let mut input = ParseInput::new("-1 ~2 3 hello"); + assert_eq!( + ColumnPos::parse_arg(&mut input).unwrap(), + ColumnPos { + x: AbsoluteOrRelative::Absolute(-1), + y: AbsoluteOrRelative::Relative(2), + z: AbsoluteOrRelative::Absolute(3) + } + ); + assert!(!input.is_done()); + input.skip_whitespace(); + assert!(input.match_next("hello")); + + let mut input = ParseInput::new("-1 2 ~3 4"); + assert_eq!( + ColumnPos::parse_arg(&mut input).unwrap(), + ColumnPos { + x: AbsoluteOrRelative::Absolute(-1), + y: AbsoluteOrRelative::Absolute(2), + z: AbsoluteOrRelative::Relative(3) + } + ); + assert!(!input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/entity_selector.rs b/crates/valence_command/src/parsers/entity_selector.rs index 8ac9e78fe..245a09a98 100644 --- a/crates/valence_command/src/parsers/entity_selector.rs +++ b/crates/valence_command/src/parsers/entity_selector.rs @@ -86,51 +86,71 @@ impl CommandArg for EntitySelector { } } -#[test] -fn test_entity_selector() { - let mut input = ParseInput::new("@e"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::SimpleSelector(EntitySelectors::AllEntities) - ); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("@e[distance=..5]"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::ComplexSelector(EntitySelectors::AllEntities, "distance=..5".to_string()) - ); - assert!(input.is_done()); + #[test] + fn test_entity_selector() { + let mut input = ParseInput::new("@e"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::SimpleSelector(EntitySelectors::AllEntities) + ); + assert!(input.is_done()); - let mut input = ParseInput::new("@s[distance=..5"); - assert!(EntitySelector::parse_arg(&mut input).is_err()); - assert!(input.is_done()); + let mut input = ParseInput::new("@e[distance=..5]"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::ComplexSelector( + EntitySelectors::AllEntities, + "distance=..5".to_owned() + ) + ); + assert!(input.is_done()); - let mut input = ParseInput::new("@r[distance=..5] hello"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::ComplexSelector(EntitySelectors::RandomPlayer, "distance=..5".to_string()) - ); - assert!(!input.is_done()); + let mut input = ParseInput::new("@s[distance=..5"); + assert!(EntitySelector::parse_arg(&mut input).is_err()); + assert!(input.is_done()); - let mut input = ParseInput::new("@p[distance=..5]hello"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::ComplexSelector(EntitySelectors::NearestPlayer, "distance=..5".to_string()) - ); - assert!(!input.is_done()); + let mut input = ParseInput::new("@r[distance=..5] hello"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::ComplexSelector( + EntitySelectors::RandomPlayer, + "distance=..5".to_owned() + ) + ); + assert!(!input.is_done()); - let mut input = ParseInput::new("@e[distance=..5] hello world"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::ComplexSelector(EntitySelectors::AllEntities, "distance=..5".to_string()) - ); - assert!(!input.is_done()); + let mut input = ParseInput::new("@p[distance=..5]hello"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::ComplexSelector( + EntitySelectors::NearestPlayer, + "distance=..5".to_owned() + ) + ); + assert!(!input.is_done()); - let mut input = ParseInput::new("@e[distance=..5]hello world"); - assert_eq!( - EntitySelector::parse_arg(&mut input).unwrap(), - EntitySelector::ComplexSelector(EntitySelectors::AllEntities, "distance=..5".to_string()) - ); - assert!(!input.is_done()); + let mut input = ParseInput::new("@e[distance=..5] hello world"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::ComplexSelector( + EntitySelectors::AllEntities, + "distance=..5".to_owned() + ) + ); + assert!(!input.is_done()); + + let mut input = ParseInput::new("@e[distance=..5]hello world"); + assert_eq!( + EntitySelector::parse_arg(&mut input).unwrap(), + EntitySelector::ComplexSelector( + EntitySelectors::AllEntities, + "distance=..5".to_owned() + ) + ); + assert!(!input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/numbers.rs b/crates/valence_command/src/parsers/numbers.rs index 9f8ad9761..fe9c3cf39 100644 --- a/crates/valence_command/src/parsers/numbers.rs +++ b/crates/valence_command/src/parsers/numbers.rs @@ -10,8 +10,8 @@ macro_rules! impl_parser_for_number { let parsed = s.parse::<$type>(); parsed.map_err(|_| CommandArgParseError::InvalidArgument { - expected: $name.to_string(), - got: s.to_string(), + expected: $name.to_owned(), + got: s.to_owned(), }) } @@ -31,25 +31,30 @@ impl_parser_for_number!(i32, "integer", Integer); impl_parser_for_number!(i64, "long", Long); impl_parser_for_number!(u32, "unsigned integer", Integer); -#[test] -fn test_number() { - let mut input = ParseInput::new("1"); - assert_eq!(1, i32::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("1"); - assert_eq!(1, i64::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); + #[test] + fn test_number() { + let mut input = ParseInput::new("1"); + assert_eq!(1, i32::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); - let mut input = ParseInput::new("1.0"); - assert_eq!(1.0, f32::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); + let mut input = ParseInput::new("1"); + assert_eq!(1, i64::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); - let mut input = ParseInput::new("1.0"); - assert_eq!(1.0, f64::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); + let mut input = ParseInput::new("1.0"); + assert_eq!(1.0, f32::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); - let mut input = ParseInput::new("3.40282347e+38 "); - assert_eq!(f32::MAX, f32::parse_arg(&mut input).unwrap()); - assert!(!input.is_done()); + let mut input = ParseInput::new("1.0"); + assert_eq!(1.0, f64::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); + + let mut input = ParseInput::new("3.40282347e+38 "); + assert_eq!(f32::MAX, f32::parse_arg(&mut input).unwrap()); + assert!(!input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/strings.rs b/crates/valence_command/src/parsers/strings.rs index 9b377af8e..1ca93265e 100644 --- a/crates/valence_command/src/parsers/strings.rs +++ b/crates/valence_command/src/parsers/strings.rs @@ -15,14 +15,6 @@ impl CommandArg for String { } } -#[test] -fn test_string() { - let mut input = ParseInput::new("hello world"); - assert_eq!("hello", String::parse_arg(&mut input).unwrap()); - assert_eq!("world", String::parse_arg(&mut input).unwrap()); - assert!(input.is_done()); -} - #[derive(Debug, Clone, PartialEq, Eq, Default, Deref)] pub struct GreedyString(pub String); @@ -43,16 +35,6 @@ impl CommandArg for GreedyString { } } -#[test] -fn test_greedy_string() { - let mut input = ParseInput::new("hello world"); - assert_eq!( - "hello world", - GreedyString::parse_arg(&mut input).unwrap().0 - ); - assert!(input.is_done()); -} - #[derive(Debug, Clone, PartialEq, Eq, Default, Deref)] pub struct QuotableString(pub String); @@ -88,20 +70,42 @@ impl CommandArg for QuotableString { } } -#[test] -fn test_quotable_string() { - let mut input = ParseInput::new("\"hello world\""); - assert_eq!( - "hello world", - QuotableString::parse_arg(&mut input).unwrap().0 - ); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_quotable_string() { + let mut input = ParseInput::new("\"hello world\""); + assert_eq!( + "hello world", + QuotableString::parse_arg(&mut input).unwrap().0 + ); + assert!(input.is_done()); - let mut input = ParseInput::new("\"hello w\"orld"); - assert_eq!("hello w", QuotableString::parse_arg(&mut input).unwrap().0); - assert!(!input.is_done()); + let mut input = ParseInput::new("\"hello w\"orld"); + assert_eq!("hello w", QuotableString::parse_arg(&mut input).unwrap().0); + assert!(!input.is_done()); - let mut input = ParseInput::new("hello world\""); - assert_eq!("hello", QuotableString::parse_arg(&mut input).unwrap().0); - assert!(!input.is_done()); + let mut input = ParseInput::new("hello world\""); + assert_eq!("hello", QuotableString::parse_arg(&mut input).unwrap().0); + assert!(!input.is_done()); + } + + #[test] + fn test_greedy_string() { + let mut input = ParseInput::new("hello world"); + assert_eq!( + "hello world", + GreedyString::parse_arg(&mut input).unwrap().0 + ); + assert!(input.is_done()); + } + #[test] + fn test_string() { + let mut input = ParseInput::new("hello world"); + assert_eq!("hello", String::parse_arg(&mut input).unwrap()); + assert_eq!("world", String::parse_arg(&mut input).unwrap()); + assert!(input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/swizzle.rs b/crates/valence_command/src/parsers/swizzle.rs index 161c94dd6..d1557e56a 100644 --- a/crates/valence_command/src/parsers/swizzle.rs +++ b/crates/valence_command/src/parsers/swizzle.rs @@ -30,88 +30,93 @@ impl CommandArg for Swizzle { } } -#[test] -fn test_swizzle() { - let mut input = ParseInput::new("xyzzzz"); - let swizzle = Swizzle::parse_arg(&mut input).unwrap(); - assert_eq!( - swizzle, - Swizzle { - x: true, - y: true, - z: true - } - ); - assert!(input.is_done()); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("xzy"); - let swizzle = Swizzle::parse_arg(&mut input).unwrap(); - assert_eq!( - swizzle, - Swizzle { - x: true, - y: true, - z: true - } - ); - assert!(input.is_done()); + #[test] + fn test_swizzle() { + let mut input = ParseInput::new("xyzzzz"); + let swizzle = Swizzle::parse_arg(&mut input).unwrap(); + assert_eq!( + swizzle, + Swizzle { + x: true, + y: true, + z: true + } + ); + assert!(input.is_done()); - let mut input = ParseInput::new("x"); - let swizzle = Swizzle::parse_arg(&mut input).unwrap(); - assert_eq!( - swizzle, - Swizzle { - x: true, - y: false, - z: false - } - ); - assert!(input.is_done()); + let mut input = ParseInput::new("xzy"); + let swizzle = Swizzle::parse_arg(&mut input).unwrap(); + assert_eq!( + swizzle, + Swizzle { + x: true, + y: true, + z: true + } + ); + assert!(input.is_done()); - let mut input = ParseInput::new("x y z zy xyz"); - let swizzle_a = Swizzle::parse_arg(&mut input).unwrap(); - let swizzle_b = Swizzle::parse_arg(&mut input).unwrap(); - let swizzle_c = Swizzle::parse_arg(&mut input).unwrap(); - let swizzle_d = Swizzle::parse_arg(&mut input).unwrap(); - let swizzle_e = Swizzle::parse_arg(&mut input).unwrap(); - assert_eq!( - swizzle_a, - Swizzle { - x: true, - y: false, - z: false - } - ); - assert_eq!( - swizzle_b, - Swizzle { - x: false, - y: true, - z: false - } - ); - assert_eq!( - swizzle_c, - Swizzle { - x: false, - y: false, - z: true - } - ); - assert_eq!( - swizzle_d, - Swizzle { - x: false, - y: true, - z: true - } - ); - assert_eq!( - swizzle_e, - Swizzle { - x: true, - y: true, - z: true - } - ); + let mut input = ParseInput::new("x"); + let swizzle = Swizzle::parse_arg(&mut input).unwrap(); + assert_eq!( + swizzle, + Swizzle { + x: true, + y: false, + z: false + } + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("x y z zy xyz"); + let swizzle_a = Swizzle::parse_arg(&mut input).unwrap(); + let swizzle_b = Swizzle::parse_arg(&mut input).unwrap(); + let swizzle_c = Swizzle::parse_arg(&mut input).unwrap(); + let swizzle_d = Swizzle::parse_arg(&mut input).unwrap(); + let swizzle_e = Swizzle::parse_arg(&mut input).unwrap(); + assert_eq!( + swizzle_a, + Swizzle { + x: true, + y: false, + z: false + } + ); + assert_eq!( + swizzle_b, + Swizzle { + x: false, + y: true, + z: false + } + ); + assert_eq!( + swizzle_c, + Swizzle { + x: false, + y: false, + z: true + } + ); + assert_eq!( + swizzle_d, + Swizzle { + x: false, + y: true, + z: true + } + ); + assert_eq!( + swizzle_e, + Swizzle { + x: true, + y: true, + z: true + } + ); + } } diff --git a/crates/valence_command/src/parsers/time.rs b/crates/valence_command/src/parsers/time.rs index 5859ae72f..f55c2c450 100644 --- a/crates/valence_command/src/parsers/time.rs +++ b/crates/valence_command/src/parsers/time.rs @@ -63,21 +63,26 @@ impl CommandArg for Time { } } -#[test] -fn test_time() { - let mut input = ParseInput::new("42.31t"); - let time = Time::parse_arg(&mut input).unwrap(); - assert_eq!(time, Time::Ticks(42.31)); +#[cfg(test)] +mod tests { + use super::*; - let mut input = ParseInput::new("42.31"); - let time = Time::parse_arg(&mut input).unwrap(); - assert_eq!(time, Time::Ticks(42.31)); + #[test] + fn test_time() { + let mut input = ParseInput::new("42.31t"); + let time = Time::parse_arg(&mut input).unwrap(); + assert_eq!(time, Time::Ticks(42.31)); - let mut input = ParseInput::new("1239.72s"); - let time = Time::parse_arg(&mut input).unwrap(); - assert_eq!(time, Time::Seconds(1239.72)); + let mut input = ParseInput::new("42.31"); + let time = Time::parse_arg(&mut input).unwrap(); + assert_eq!(time, Time::Ticks(42.31)); - let mut input = ParseInput::new("133.1d"); - let time = Time::parse_arg(&mut input).unwrap(); - assert_eq!(time, Time::Days(133.1)); + let mut input = ParseInput::new("1239.72s"); + let time = Time::parse_arg(&mut input).unwrap(); + assert_eq!(time, Time::Seconds(1239.72)); + + let mut input = ParseInput::new("133.1d"); + let time = Time::parse_arg(&mut input).unwrap(); + assert_eq!(time, Time::Days(133.1)); + } } diff --git a/crates/valence_command/src/parsers/vec2.rs b/crates/valence_command/src/parsers/vec2.rs index 02235a8cf..8b63aefcb 100644 --- a/crates/valence_command/src/parsers/vec2.rs +++ b/crates/valence_command/src/parsers/vec2.rs @@ -22,35 +22,40 @@ impl CommandArg for Vec2 { } } -#[test] -fn test_vec2() { - let mut input = ParseInput::new("~-1.5 2.5"); - assert_eq!( - Vec2::parse_arg(&mut input).unwrap(), - Vec2 { - x: AbsoluteOrRelative::Relative(-1.5), - y: AbsoluteOrRelative::Absolute(2.5), - } - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("-1.5 ~2.5 "); - assert_eq!( - Vec2::parse_arg(&mut input).unwrap(), - Vec2 { - x: AbsoluteOrRelative::Absolute(-1.5), - y: AbsoluteOrRelative::Relative(2.5), - } - ); - assert!(!input.is_done()); - - let mut input = ParseInput::new("-1.5 2.5 3.5"); - assert_eq!( - Vec2::parse_arg(&mut input).unwrap(), - Vec2 { - x: AbsoluteOrRelative::Absolute(-1.5), - y: AbsoluteOrRelative::Absolute(2.5), - } - ); - assert!(!input.is_done()); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_vec2() { + let mut input = ParseInput::new("~-1.5 2.5"); + assert_eq!( + Vec2::parse_arg(&mut input).unwrap(), + Vec2 { + x: AbsoluteOrRelative::Relative(-1.5), + y: AbsoluteOrRelative::Absolute(2.5), + } + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("-1.5 ~2.5 "); + assert_eq!( + Vec2::parse_arg(&mut input).unwrap(), + Vec2 { + x: AbsoluteOrRelative::Absolute(-1.5), + y: AbsoluteOrRelative::Relative(2.5), + } + ); + assert!(!input.is_done()); + + let mut input = ParseInput::new("-1.5 2.5 3.5"); + assert_eq!( + Vec2::parse_arg(&mut input).unwrap(), + Vec2 { + x: AbsoluteOrRelative::Absolute(-1.5), + y: AbsoluteOrRelative::Absolute(2.5), + } + ); + assert!(!input.is_done()); + } } diff --git a/crates/valence_command/src/parsers/vec3.rs b/crates/valence_command/src/parsers/vec3.rs index f5befdbed..d701986ae 100644 --- a/crates/valence_command/src/parsers/vec3.rs +++ b/crates/valence_command/src/parsers/vec3.rs @@ -22,38 +22,43 @@ impl CommandArg for Vec3 { } } -#[test] -fn test_vec3() { - let mut input = ParseInput::new("~-1.5 2.5 3.5"); - assert_eq!( - Vec3::parse_arg(&mut input).unwrap(), - Vec3 { - x: AbsoluteOrRelative::Relative(-1.5), - y: AbsoluteOrRelative::Absolute(2.5), - z: AbsoluteOrRelative::Absolute(3.5) - } - ); - assert!(input.is_done()); - - let mut input = ParseInput::new("-1.5 ~2.5 3.5 "); - assert_eq!( - Vec3::parse_arg(&mut input).unwrap(), - Vec3 { - x: AbsoluteOrRelative::Absolute(-1.5), - y: AbsoluteOrRelative::Relative(2.5), - z: AbsoluteOrRelative::Absolute(3.5) - } - ); - assert!(!input.is_done()); - - let mut input = ParseInput::new("-1.5 2.5 ~3.5 4.5"); - assert_eq!( - Vec3::parse_arg(&mut input).unwrap(), - Vec3 { - x: AbsoluteOrRelative::Absolute(-1.5), - y: AbsoluteOrRelative::Absolute(2.5), - z: AbsoluteOrRelative::Relative(3.5) - } - ); - assert!(!input.is_done()); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_vec3() { + let mut input = ParseInput::new("~-1.5 2.5 3.5"); + assert_eq!( + Vec3::parse_arg(&mut input).unwrap(), + Vec3 { + x: AbsoluteOrRelative::Relative(-1.5), + y: AbsoluteOrRelative::Absolute(2.5), + z: AbsoluteOrRelative::Absolute(3.5) + } + ); + assert!(input.is_done()); + + let mut input = ParseInput::new("-1.5 ~2.5 3.5 "); + assert_eq!( + Vec3::parse_arg(&mut input).unwrap(), + Vec3 { + x: AbsoluteOrRelative::Absolute(-1.5), + y: AbsoluteOrRelative::Relative(2.5), + z: AbsoluteOrRelative::Absolute(3.5) + } + ); + assert!(!input.is_done()); + + let mut input = ParseInput::new("-1.5 2.5 ~3.5 4.5"); + assert_eq!( + Vec3::parse_arg(&mut input).unwrap(), + Vec3 { + x: AbsoluteOrRelative::Absolute(-1.5), + y: AbsoluteOrRelative::Absolute(2.5), + z: AbsoluteOrRelative::Relative(3.5) + } + ); + assert!(!input.is_done()); + } } diff --git a/crates/valence_nbt/src/binary/tests.rs b/crates/valence_nbt/src/binary/tests.rs index f335dbcac..f100b167c 100644 --- a/crates/valence_nbt/src/binary/tests.rs +++ b/crates/valence_nbt/src/binary/tests.rs @@ -9,14 +9,14 @@ fn round_trip() { let compound = example_compound(); - to_binary(&mut buf, ROOT_NAME, &compound).unwrap(); + to_binary(&compound, &mut buf, ROOT_NAME).unwrap(); println!("{buf:?}"); - let (root_name, decoded) = from_binary(&mut buf.as_slice()).unwrap().unwrap(); + let (decoded, root_name) = from_binary(&mut buf.as_slice()).unwrap(); assert_eq!(root_name, ROOT_NAME); - assert_eq!(Value::from(compound), decoded); + assert_eq!(compound, decoded); } #[test] @@ -28,7 +28,7 @@ fn check_min_sizes() { let dbg = format!("{min_val:?}"); let mut buf = vec![]; - to_binary(&mut buf, "", &compound!("" => min_val)).unwrap(); + to_binary(&compound!("" => min_val), &mut buf, "").unwrap(); assert_eq!( expected_size, @@ -65,7 +65,7 @@ fn deeply_nested_compound_decode() { buf.push(Tag::End as u8); // End root compound // Should not overflow the stack - let _ = from_binary::<_, String>(&mut buf.as_slice()); + let _ = from_binary::(&mut buf.as_slice()); } #[test] @@ -84,7 +84,7 @@ fn deeply_nested_list_decode() { buf.push(Tag::End as u8); // End root compound // Should not overflow the stack - let _ = from_binary::<_, String>(&mut buf.as_slice()); + let _ = from_binary::(&mut buf.as_slice()); } fn example_compound() -> Compound { diff --git a/crates/valence_protocol/src/lib.rs b/crates/valence_protocol/src/lib.rs index ab9e59437..0306a92e5 100644 --- a/crates/valence_protocol/src/lib.rs +++ b/crates/valence_protocol/src/lib.rs @@ -328,7 +328,7 @@ mod tests { #[derive(Encode, Decode, Packet, Debug)] #[packet(id = 3, side = PacketSide::Clientbound)] - struct EmptyStruct {} + struct EmptyStruct; #[derive(Encode, Decode, Packet, Debug)] #[packet(id = 4, side = PacketSide::Clientbound)] diff --git a/crates/valence_protocol/src/velocity.rs b/crates/valence_protocol/src/velocity.rs index 5e3a9ff1b..325c223b3 100644 --- a/crates/valence_protocol/src/velocity.rs +++ b/crates/valence_protocol/src/velocity.rs @@ -46,8 +46,8 @@ impl fmt::Display for Velocity { #[cfg(test)] #[test] fn velocity_from_ms() { - let val_1 = Velocity::from_ms_f32([(); 3].map(|_| -3.3575)).0[0]; - let val_2 = Velocity::from_ms_f64([(); 3].map(|_| -3.3575)).0[0]; + let val_1 = Velocity::from_ms_f32([(); 3].map(|()| -3.3575)).0[0]; + let val_2 = Velocity::from_ms_f64([(); 3].map(|()| -3.3575)).0[0]; assert_eq!(val_1, val_2); assert_eq!(val_1, -1343); diff --git a/crates/valence_scoreboard/src/lib.rs b/crates/valence_scoreboard/src/lib.rs index 2dc161853..5d0beff7b 100644 --- a/crates/valence_scoreboard/src/lib.rs +++ b/crates/valence_scoreboard/src/lib.rs @@ -269,6 +269,6 @@ fn update_scores( layer.write_packet(&packet); } - old_scores.0 = scores.0.clone(); + old_scores.0.clone_from(&scores.0); } } diff --git a/crates/valence_server/src/chunk_view.rs b/crates/valence_server/src/chunk_view.rs index a92a808a3..dd454315c 100644 --- a/crates/valence_server/src/chunk_view.rs +++ b/crates/valence_server/src/chunk_view.rs @@ -90,7 +90,7 @@ mod tests { #[test] fn chunk_view_contains() { let view = ChunkView::new(ChunkPos::new(0, 0), 32); - let positions = BTreeSet::from_iter(view.iter()); + let positions = view.iter().collect::>(); for z in -64..64 { for x in -64..64 { diff --git a/crates/valence_server/src/layer/chunk/loaded.rs b/crates/valence_server/src/layer/chunk/loaded.rs index daa23f685..02c801b19 100644 --- a/crates/valence_server/src/layer/chunk/loaded.rs +++ b/crates/valence_server/src/layer/chunk/loaded.rs @@ -273,7 +273,7 @@ impl LoadedChunk { self.changed_biomes = false; messages.send_local_infallible(LocalMsg::ChangeBiome { pos }, |buf| { - for sect in &self.sections { + for sect in self.sections.iter() { sect.biomes .encode_mc_format( &mut *buf, @@ -390,7 +390,7 @@ impl LoadedChunk { let mut blocks_and_biomes: Vec = vec![]; - for sect in &self.sections { + for sect in self.sections.iter() { sect.count_non_air_blocks() .encode(&mut blocks_and_biomes) .unwrap(); @@ -463,7 +463,7 @@ impl LoadedChunk { assert!(!self.changed_biomes); assert!(self.changed_block_entities.is_empty()); - for sect in &self.sections { + for sect in self.sections.iter() { assert!(sect.updates.is_empty()); } } @@ -686,7 +686,7 @@ impl Chunk for LoadedChunk { fn shrink_to_fit(&mut self) { self.cached_init_packets.get_mut().shrink_to_fit(); - for sect in &mut self.sections { + for sect in self.sections.iter_mut() { sect.block_states.shrink_to_fit(); sect.biomes.shrink_to_fit(); sect.updates.shrink_to_fit(); diff --git a/crates/valence_spatial/src/bvh.rs b/crates/valence_spatial/src/bvh.rs index 669262094..81b3476cf 100644 --- a/crates/valence_spatial/src/bvh.rs +++ b/crates/valence_spatial/src/bvh.rs @@ -35,7 +35,7 @@ impl Bvh { } } - pub fn rebuild(&mut self, leaves: impl IntoIterator) { + pub fn rebuild>(&mut self, leaves: I) { self.internal_nodes.clear(); self.leaf_nodes.clear(); diff --git a/examples/ctf.rs b/examples/ctf.rs index 99ea06cf6..8339bd644 100644 --- a/examples/ctf.rs +++ b/examples/ctf.rs @@ -728,13 +728,13 @@ struct FlagManager { #[derive(Debug, Resource)] struct CtfGlobals { - pub scoreboard_layer: Entity, + pub(crate) scoreboard_layer: Entity, - pub red_flag: BlockPos, - pub blue_flag: BlockPos, + pub(crate) red_flag: BlockPos, + pub(crate) blue_flag: BlockPos, - pub red_capture_trigger: TriggerArea, - pub blue_capture_trigger: TriggerArea, + pub(crate) red_capture_trigger: TriggerArea, + pub(crate) blue_capture_trigger: TriggerArea, } fn update_flag_visuals( @@ -841,9 +841,9 @@ struct CtfLayers { /// should be viewed. /// /// This is used to make friendly players glow. - pub friendly_layers: HashMap, + pub(crate) friendly_layers: HashMap, /// Ditto, but for enemy players. - pub enemy_layers: HashMap, + pub(crate) enemy_layers: HashMap, } impl CtfLayers { diff --git a/tools/packet_inspector/build.rs b/tools/packet_inspector/build.rs index 9a64b69f8..83cad2965 100644 --- a/tools/packet_inspector/build.rs +++ b/tools/packet_inspector/build.rs @@ -43,14 +43,14 @@ fn write_packets(packets: &Vec) -> anyhow::Result<()> { // if the packet is clientbound, but the name does not ends with S2c, add it let name = if packet.side == "clientbound" && !name.ends_with("S2c") { - format!("{}S2c", name) + format!("{name}S2c") } else { name }; // same for serverbound let name = if packet.side == "serverbound" && !name.ends_with("C2s") { - format!("{}C2s", name) + format!("{name}C2s") } else { name }; @@ -101,16 +101,16 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> { let mut grouped_packets = packets.iter().fold(grouped_packets, |mut acc, packet| { let side = match packet.side.as_str() { - "serverbound" => "Serverbound".to_string(), - "clientbound" => "Clientbound".to_string(), + "serverbound" => "Serverbound".to_owned(), + "clientbound" => "Clientbound".to_owned(), _ => panic!("Invalid side"), }; let state = match packet.state.as_str() { - "handshaking" => "Handshaking".to_string(), - "status" => "Status".to_string(), - "login" => "Login".to_string(), - "play" => "Play".to_string(), + "handshaking" => "Handshaking".to_owned(), + "status" => "Status".to_owned(), + "login" => "Login".to_owned(), + "play" => "Play".to_owned(), _ => panic!("Invalid state"), }; @@ -118,7 +118,7 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> { .name .strip_suffix("Packet") .unwrap_or(&packet.name) - .to_string(); + .to_owned(); // lowercase the last character of name let name = { @@ -132,14 +132,14 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> { // if the packet is clientbound, but the name does not ends with S2c, add it let name = if side == "Clientbound" && !name.ends_with("S2c") { - format!("{}S2c", name) + format!("{name}S2c") } else { name }; // same for serverbound let name = if side == "Serverbound" && !name.ends_with("C2s") { - format!("{}C2s", name) + format!("{name}C2s") } else { name }; @@ -153,7 +153,7 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> { let mut generated = TokenStream::new(); - for (side, state_map) in grouped_packets.iter_mut() { + for (side, state_map) in &mut grouped_packets { let mut side_arms = TokenStream::new(); for (state, id_map) in state_map.iter_mut() { let mut match_arms = TokenStream::new(); diff --git a/tools/packet_inspector/src/app.rs b/tools/packet_inspector/src/app.rs index 5792645ff..c23ac107f 100644 --- a/tools/packet_inspector/src/app.rs +++ b/tools/packet_inspector/src/app.rs @@ -104,8 +104,8 @@ impl GuiApp { }; Self { - shared_state, tree, + shared_state, tab_viewer, } } diff --git a/tools/packet_inspector/src/app/connection.rs b/tools/packet_inspector/src/app/connection.rs index e665f1b94..d3a53dca4 100644 --- a/tools/packet_inspector/src/app/connection.rs +++ b/tools/packet_inspector/src/app/connection.rs @@ -1,7 +1,7 @@ use super::{SharedState, Tab, View}; use crate::shared_state::Event; -pub(crate) struct Connection {} +pub(crate) struct Connection; impl Tab for Connection { fn new() -> Self { diff --git a/tools/packet_inspector/src/app/filter.rs b/tools/packet_inspector/src/app/filter.rs index e6af0cb95..9bc3f4dca 100644 --- a/tools/packet_inspector/src/app/filter.rs +++ b/tools/packet_inspector/src/app/filter.rs @@ -144,9 +144,9 @@ fn draw_packet_list(ui: &mut Ui, state: &mut SharedState, packet_state: PacketSt } fn int_to_hex(i: i32) -> String { - format!("0x{:0>2X}", i) + format!("0x{i:0>2X}") } fn int_to_hex_lower(i: i32) -> String { - format!("0x{:0>2x}", i) + format!("0x{i:0>2x}") } diff --git a/tools/packet_inspector/src/app/hex_viewer.rs b/tools/packet_inspector/src/app/hex_viewer.rs index d14894cde..0c737e14a 100644 --- a/tools/packet_inspector/src/app/hex_viewer.rs +++ b/tools/packet_inspector/src/app/hex_viewer.rs @@ -16,7 +16,7 @@ impl Tab for HexView { impl View for HexView { fn ui(&mut self, ui: &mut egui::Ui, state: &mut SharedState) { - let mut buf = [0u8; 16]; + let mut buf = [0_u8; 16]; let mut count = 0; let packets = state.packets.read().unwrap(); @@ -34,7 +34,7 @@ impl View for HexView { .show(ui, |ui| { ui.label(" "); for i in 0..16 { - ui.label(format!("{:02X}", i)); + ui.label(format!("{i:02X}")); } ui.end_row(); loop { @@ -43,14 +43,14 @@ impl View for HexView { break; } - ui.label(format!("{:08X}", count)); + ui.label(format!("{count:08X}")); let text_color = if ui.style().visuals.dark_mode { egui::Color32::from_gray(255) } else { egui::Color32::from_gray(0) }; for b in buf.iter().take(bytes_read) { - ui.colored_label(text_color, format!("{:02X}", b)); + ui.colored_label(text_color, format!("{b:02X}")); } for _ in 0..16 - bytes_read { ui.label(" "); diff --git a/tools/packet_inspector/src/app/packet_list.rs b/tools/packet_inspector/src/app/packet_list.rs index 602d154fd..c8adbc537 100644 --- a/tools/packet_inspector/src/app/packet_list.rs +++ b/tools/packet_inspector/src/app/packet_list.rs @@ -81,7 +81,7 @@ fn draw_packet_counter(state: &SharedState, ui: &mut Ui) { .filter(|p| state.packet_filter.get(p).unwrap_or(true)) .count(); - ui.label(format!("({}/{})", filtered_packets, length)); + ui.label(format!("({filtered_packets}/{length})",)); } fn draw_clear_button(state: &mut SharedState, ui: &mut Ui) { @@ -135,14 +135,16 @@ fn draw_packet_widget(ui: &mut Ui, packet: &Packet, selected: bool) -> Response Sense::click(), ); // this should give me a new rect inside the scroll area... no? - let fill = match selected /*packet.selected*/ { - true => Rgba::from_rgba_premultiplied(0.1, 0.1, 0.1, 0.5), - false => Rgba::from_rgba_premultiplied(0.0, 0.0, 0.0, 0.0), + let fill = if selected { + Rgba::from_rgba_premultiplied(0.1, 0.1, 0.1, 0.5) + } else { + Rgba::from_rgba_premultiplied(0.0, 0.0, 0.0, 0.0) }; - let text_color: Color32 = match selected /*packet.selected*/ { - true => Rgba::from_rgba_premultiplied(0.0, 0.0, 0.0, 1.0).into(), - false => ui.visuals().strong_text_color(), + let text_color: Color32 = if selected { + Rgba::from_rgba_premultiplied(0.0, 0.0, 0.0, 1.0).into() + } else { + ui.visuals().strong_text_color() }; if ui.is_rect_visible(rect) { diff --git a/tools/packet_inspector/src/app/text_viewer.rs b/tools/packet_inspector/src/app/text_viewer.rs index 610a2816b..1af20594c 100644 --- a/tools/packet_inspector/src/app/text_viewer.rs +++ b/tools/packet_inspector/src/app/text_viewer.rs @@ -20,7 +20,7 @@ impl Tab for TextView { fn new() -> Self { Self { last_packet_id: None, - packet_str: "".to_string(), + packet_str: String::new(), } } @@ -34,7 +34,7 @@ impl View for TextView { let packets = state.packets.read().unwrap(); let Some(packet_index) = state.selected_packet else { self.last_packet_id = None; - self.packet_str = "".to_string(); + self.packet_str = String::new(); return; }; @@ -43,7 +43,7 @@ impl View for TextView { self.packet_str = match utils::packet_to_string(&packets[packet_index]) { Ok(str) => str, - Err(err) => format!("Error: {}", err), + Err(err) => format!("Error: {err}"), }; } @@ -123,7 +123,7 @@ impl SyntectTheme { .copied() } - fn name(&self) -> &'static str { + fn name(self) -> &'static str { match self { Self::Base16EightiesDark => "Base16 Eighties (dark)", Self::Base16MochaDark => "Base16 Mocha (dark)", @@ -135,7 +135,7 @@ impl SyntectTheme { } } - fn syntect_key_name(&self) -> &'static str { + fn syntect_key_name(self) -> &'static str { match self { Self::Base16EightiesDark => "base16-eighties.dark", Self::Base16MochaDark => "base16-mocha.dark", @@ -147,7 +147,7 @@ impl SyntectTheme { } } - pub(crate) fn is_dark(&self) -> bool { + pub(crate) fn is_dark(self) -> bool { match self { Self::Base16EightiesDark | Self::Base16MochaDark diff --git a/tools/packet_inspector/src/lib.rs b/tools/packet_inspector/src/lib.rs index dd21474f4..e1edac034 100644 --- a/tools/packet_inspector/src/lib.rs +++ b/tools/packet_inspector/src/lib.rs @@ -103,7 +103,7 @@ impl Proxy { _ = self.main_task => {}, // If the main task doesn't stop after 5 seconds, we force terminate it - _ = tokio::time::sleep(Duration::from_secs(5)) => { + () = tokio::time::sleep(Duration::from_secs(5)) => { abort_handle.abort(); }, } diff --git a/tools/packet_inspector/src/shared_state.rs b/tools/packet_inspector/src/shared_state.rs index 0981a3026..e9ca1c888 100644 --- a/tools/packet_inspector/src/shared_state.rs +++ b/tools/packet_inspector/src/shared_state.rs @@ -15,7 +15,7 @@ impl PacketFilter { pub(crate) fn new() -> Self { let mut inner = HashMap::new(); - for p in packet_inspector::STD_PACKETS.iter() { + for p in &packet_inspector::STD_PACKETS { inner.insert(p.clone(), true); } @@ -51,25 +51,25 @@ pub(crate) enum Event { #[derive(serde::Deserialize, serde::Serialize)] pub(crate) struct SharedState { - pub listener_addr: String, - pub server_addr: String, - pub autostart: bool, - pub packet_filter: PacketFilter, - pub packet_search: String, + pub(crate) listener_addr: String, + pub(crate) server_addr: String, + pub(crate) autostart: bool, + pub(crate) packet_filter: PacketFilter, + pub(crate) packet_search: String, #[serde(skip)] - pub is_listening: bool, + pub(crate) is_listening: bool, #[serde(skip)] - pub selected_packet: Option, + pub(crate) selected_packet: Option, #[serde(skip)] - pub update_scroll: bool, + pub(crate) update_scroll: bool, #[serde(skip)] - pub packets: RwLock>, + pub(crate) packets: RwLock>, #[serde(skip)] pub(super) receiver: Option>, #[serde(skip)] sender: Option>, #[serde(skip)] - pub ctx: Option, + pub(crate) ctx: Option, } impl Default for SharedState { @@ -77,8 +77,8 @@ impl Default for SharedState { let (sender, receiver) = flume::unbounded(); Self { - listener_addr: "127.0.0.1:25566".to_string(), - server_addr: "127.0.0.1:25565".to_string(), + listener_addr: "127.0.0.1:25566".to_owned(), + server_addr: "127.0.0.1:25565".to_owned(), autostart: false, is_listening: false, packet_search: String::new(), @@ -110,7 +110,7 @@ impl SharedState { let mut packet_filter = PacketFilter::new(); // iterate over packet_inspector::STD_PACKETS - for p in packet_inspector::STD_PACKETS.iter() { + for p in &packet_inspector::STD_PACKETS { // if the packet is in the current packet_filter if let Some(v) = self.packet_filter.get(p) { // insert it into packet_filter diff --git a/tools/packet_inspector/src/tri_checkbox.rs b/tools/packet_inspector/src/tri_checkbox.rs index ccd86fbf4..cc92466d4 100644 --- a/tools/packet_inspector/src/tri_checkbox.rs +++ b/tools/packet_inspector/src/tri_checkbox.rs @@ -118,7 +118,7 @@ impl<'a> Widget for TriCheckbox<'a> { visuals.fg_stroke, )); } - _ => {} + TriCheckboxState::Disabled => {} } if let Some(text) = text { let text_pos = pos2( diff --git a/tools/playground/src/playground.template.rs b/tools/playground/src/playground.template.rs index 01476ed59..a157355ea 100644 --- a/tools/playground/src/playground.template.rs +++ b/tools/playground/src/playground.template.rs @@ -69,7 +69,7 @@ fn init_clients( layer_id.0 = layer; visible_chunk_layer.0 = layer; visible_entity_layers.0.insert(layer); - pos.set([0.0, SPAWN_Y as f64 + 1.0, 0.0]); + pos.set([0.0, f64::from(SPAWN_Y) + 1.0, 0.0]); *game_mode = GameMode::Creative; } } diff --git a/tools/stresser/src/args.rs b/tools/stresser/src/args.rs index e7e33d7d4..936c2c1bc 100644 --- a/tools/stresser/src/args.rs +++ b/tools/stresser/src/args.rs @@ -5,25 +5,25 @@ use clap::{arg, command, Parser}; pub(crate) struct StresserArgs { /// IPv4/IPv6/DNS address of a server. #[arg(short = 't', long = "target")] - pub target_host: String, + pub(crate) target_host: String, /// Number of sessions. #[arg(short = 'c', long = "count")] - pub sessions_count: usize, + pub(crate) sessions_count: usize, /// Name prefix of sessions. #[arg(default_value = "Stresser")] #[arg(short = 'n', long = "name")] - pub name_prefix: String, + pub(crate) name_prefix: String, /// Spawn cooldown of sessions in milliseconds. /// The lower the value, the more frequently sessions are spawned. #[arg(default_value = "10")] #[arg(long = "cooldown")] - pub spawn_cooldown: u64, + pub(crate) spawn_cooldown: u64, /// Read buffer size in bytes. #[arg(default_value = "4096")] #[arg(long = "read-buffer")] - pub read_buffer_size: usize, + pub(crate) read_buffer_size: usize, } diff --git a/tools/stresser/src/stresser.rs b/tools/stresser/src/stresser.rs index 13181a53b..3e7c03060 100644 --- a/tools/stresser/src/stresser.rs +++ b/tools/stresser/src/stresser.rs @@ -73,7 +73,7 @@ pub async fn make_session<'a>(params: &SessionParams<'a>) -> anyhow::Result<()> match conn.try_read_buf(&mut read_buf) { Ok(0) => return Err(io::Error::from(ErrorKind::UnexpectedEof).into()), - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => continue, + Err(e) if e.kind() == io::ErrorKind::WouldBlock => continue, Err(e) => return Err(e.into()), Ok(_) => (), }; @@ -143,7 +143,7 @@ pub async fn make_session<'a>(params: &SessionParams<'a>) -> anyhow::Result<()> match conn.try_read_buf(&mut read_buf) { Ok(0) => return Err(io::Error::from(ErrorKind::UnexpectedEof).into()), - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => continue, + Err(e) if e.kind() == io::ErrorKind::WouldBlock => continue, Err(e) => return Err(e.into()), Ok(_) => (), };