Skip to content

Commit e8d7570

Browse files
refactor(pdu)!: fix unwrap_used clippy lint warnings (#964)
Co-authored-by: Benoît CORTIER <git.divisible626@passmail.com>
1 parent 4beab02 commit e8d7570

39 files changed

+863
-301
lines changed

crates/ironrdp-acceptor/src/connection.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,10 @@ impl Sequence for Acceptor {
381381

382382
debug!(message = ?settings_initial, "Received");
383383

384-
let early_capability = settings_initial
385-
.conference_create_request
386-
.gcc_blocks
387-
.core
388-
.optional_data
389-
.early_capability_flags;
390-
391-
let joined: Vec<_> = settings_initial
392-
.conference_create_request
393-
.gcc_blocks
384+
let gcc_blocks = settings_initial.conference_create_request.into_gcc_blocks();
385+
let early_capability = gcc_blocks.core.optional_data.early_capability_flags;
386+
387+
let joined: Vec<_> = gcc_blocks
394388
.network
395389
.map(|network| {
396390
network
@@ -450,10 +444,8 @@ impl Sequence for Acceptor {
450444
);
451445

452446
let settings_response = mcs::ConnectResponse {
453-
conference_create_response: gcc::ConferenceCreateResponse {
454-
user_id: self.user_channel_id,
455-
gcc_blocks: server_blocks,
456-
},
447+
conference_create_response: gcc::ConferenceCreateResponse::new(self.user_channel_id, server_blocks)
448+
.map_err(ConnectorError::decode)?,
457449
called_connect_id: 1,
458450
domain_parameters: mcs::DomainParameters::target(),
459451
};

crates/ironrdp-connector/src/connection.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ impl Sequence for ClientConnector {
345345
let client_gcc_blocks =
346346
create_gcc_blocks(&self.config, selected_protocol, self.static_channels.values())?;
347347

348-
let connect_initial = mcs::ConnectInitial::with_gcc_blocks(client_gcc_blocks);
348+
let connect_initial =
349+
mcs::ConnectInitial::with_gcc_blocks(client_gcc_blocks).map_err(ConnectorError::decode)?;
349350

350351
debug!(message = ?connect_initial, "Send");
351352

@@ -365,9 +366,9 @@ impl Sequence for ClientConnector {
365366

366367
debug!(message = ?connect_response, "Received");
367368

368-
let client_gcc_blocks = &connect_initial.conference_create_request.gcc_blocks;
369+
let client_gcc_blocks = connect_initial.conference_create_request.gcc_blocks();
369370

370-
let server_gcc_blocks = connect_response.conference_create_response.gcc_blocks;
371+
let server_gcc_blocks = connect_response.conference_create_response.into_gcc_blocks();
371372

372373
if client_gcc_blocks.security == gcc::ClientSecurityData::no_security()
373374
&& server_gcc_blocks.security != gcc::ServerSecurityData::no_security()

crates/ironrdp-pdu/src/basic_output/fast_path.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use ironrdp_core::{
77
decode_cursor, ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeError, DecodeResult, Encode,
88
EncodeResult, InvalidFieldErr as _, ReadCursor, WriteCursor,
99
};
10-
use num_derive::{FromPrimitive, ToPrimitive};
11-
use num_traits::{FromPrimitive as _, ToPrimitive as _};
10+
use num_derive::FromPrimitive;
11+
use num_traits::FromPrimitive as _;
1212

1313
use super::bitmap::BitmapUpdateData;
1414
use super::pointer::PointerUpdateData;
@@ -136,15 +136,15 @@ impl Encode for FastPathUpdatePdu<'_> {
136136
}
137137

138138
let mut header = 0u8;
139-
header.set_bits(0..4, self.update_code.to_u8().unwrap());
140-
header.set_bits(4..6, self.fragmentation.to_u8().unwrap());
139+
header.set_bits(0..4, self.update_code.as_u8());
140+
header.set_bits(4..6, self.fragmentation.as_u8());
141141

142142
dst.write_u8(header);
143143

144144
if self.compression_flags.is_some() {
145145
header.set_bits(6..8, Compression::COMPRESSION_USED.bits());
146-
let compression_flags_with_type = self.compression_flags.map(|f| f.bits()).unwrap_or(0)
147-
| self.compression_type.and_then(|f| f.to_u8()).unwrap_or(0);
146+
let compression_flags_with_type =
147+
self.compression_flags.map(|f| f.bits()).unwrap_or(0) | self.compression_type.map_or(0, |f| f.as_u8());
148148
dst.write_u8(compression_flags_with_type);
149149
}
150150

@@ -312,7 +312,8 @@ impl Encode for FastPathUpdate<'_> {
312312
}
313313
}
314314

315-
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
315+
#[repr(u8)]
316+
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive)]
316317
pub enum UpdateCode {
317318
Orders = 0x0,
318319
Bitmap = 0x1,
@@ -328,6 +329,16 @@ pub enum UpdateCode {
328329
LargePointer = 0xc,
329330
}
330331

332+
impl UpdateCode {
333+
#[expect(
334+
clippy::as_conversions,
335+
reason = "guarantees discriminant layout, and as is the only way to cast enum -> primitive"
336+
)]
337+
pub fn as_u8(self) -> u8 {
338+
self as u8
339+
}
340+
}
341+
331342
impl From<&FastPathUpdate<'_>> for UpdateCode {
332343
fn from(update: &FastPathUpdate<'_>) -> Self {
333344
match update {
@@ -346,14 +357,25 @@ impl From<&FastPathUpdate<'_>> for UpdateCode {
346357
}
347358
}
348359

349-
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
360+
#[repr(u8)]
361+
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive)]
350362
pub enum Fragmentation {
351363
Single = 0x0,
352364
Last = 0x1,
353365
First = 0x2,
354366
Next = 0x3,
355367
}
356368

369+
impl Fragmentation {
370+
#[expect(
371+
clippy::as_conversions,
372+
reason = "guarantees discriminant layout, and as is the only way to cast enum -> primitive"
373+
)]
374+
pub fn as_u8(self) -> u8 {
375+
self as u8
376+
}
377+
}
378+
357379
bitflags! {
358380
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
359381
pub struct EncryptionFlags: u8 {

crates/ironrdp-pdu/src/basic_output/surface_commands.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ironrdp_core::{
77
WriteCursor,
88
};
99
use num_derive::{FromPrimitive, ToPrimitive};
10-
use num_traits::{FromPrimitive as _, ToPrimitive as _};
10+
use num_traits::FromPrimitive as _;
1111

1212
use crate::geometry::ExclusiveRectangle;
1313

@@ -31,7 +31,7 @@ impl Encode for SurfaceCommand<'_> {
3131
ensure_size!(in: dst, size: self.size());
3232

3333
let cmd_type = SurfaceCommandType::from(self);
34-
dst.write_u16(cmd_type.to_u16().unwrap());
34+
dst.write_u16(cmd_type.as_u16());
3535

3636
match self {
3737
Self::SetSurfaceBits(pdu) | Self::StreamSurfaceBits(pdu) => pdu.encode(dst),
@@ -324,14 +324,24 @@ impl Decode<'_> for BitmapDataHeader {
324324
}
325325
}
326326

327-
#[derive(Debug, Copy, Clone, PartialEq, FromPrimitive, ToPrimitive)]
327+
#[derive(Debug, Copy, Clone, PartialEq, FromPrimitive)]
328328
#[repr(u16)]
329329
enum SurfaceCommandType {
330330
SetSurfaceBits = 0x01,
331331
FrameMarker = 0x04,
332332
StreamSurfaceBits = 0x06,
333333
}
334334

335+
impl SurfaceCommandType {
336+
#[expect(
337+
clippy::as_conversions,
338+
reason = "guarantees discriminant layout, and as is the only way to cast enum -> primitive"
339+
)]
340+
fn as_u16(self) -> u16 {
341+
self as u16
342+
}
343+
}
344+
335345
impl From<&SurfaceCommand<'_>> for SurfaceCommandType {
336346
fn from(command: &SurfaceCommand<'_>) -> Self {
337347
match command {

crates/ironrdp-pdu/src/codecs/rfx.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use ironrdp_core::{
55
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult,
66
ReadCursor, WriteCursor,
77
};
8-
use num_derive::{FromPrimitive, ToPrimitive};
9-
use num_traits::{FromPrimitive as _, ToPrimitive as _};
8+
use num_derive::FromPrimitive;
9+
use num_traits::FromPrimitive as _;
1010

1111
use crate::rdp::capability_sets::{RfxCaps, RfxCapset};
1212

@@ -187,7 +187,7 @@ impl Encode for BlockHeader {
187187
fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
188188
ensure_fixed_part_size!(in: dst);
189189

190-
dst.write_u16(self.ty.to_u16().unwrap());
190+
dst.write_u16(self.ty.as_u16());
191191
dst.write_u32(cast_length!("data len", self.data_length)?);
192192

193193
Ok(())
@@ -307,7 +307,7 @@ impl<'de> Decode<'de> for FrameAcknowledgePdu {
307307
}
308308
}
309309

310-
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
310+
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive)]
311311
#[repr(u16)]
312312
pub enum BlockType {
313313
Tile = 0xCAC3,
@@ -330,4 +330,12 @@ impl BlockType {
330330
BlockType::Context | BlockType::FrameBegin | BlockType::FrameEnd | BlockType::Region | BlockType::Extension
331331
)
332332
}
333+
334+
#[expect(
335+
clippy::as_conversions,
336+
reason = "guarantees discriminant layout, and as is the only way to cast enum -> primitive"
337+
)]
338+
fn as_u16(self) -> u16 {
339+
self as u16
340+
}
333341
}

crates/ironrdp-pdu/src/codecs/rfx/data_messages.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use ironrdp_core::{
44
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult,
55
ReadCursor, WriteCursor,
66
};
7-
use num_derive::{FromPrimitive, ToPrimitive};
8-
use num_traits::{FromPrimitive as _, ToPrimitive as _};
7+
use num_derive::FromPrimitive;
8+
use num_traits::FromPrimitive as _;
99

1010
use crate::codecs::rfx::Block;
1111

@@ -48,7 +48,7 @@ impl Encode for ContextPdu {
4848
properties.set_bits(0..3, self.flags.bits());
4949
properties.set_bits(3..5, COLOR_CONVERSION_ICT);
5050
properties.set_bits(5..9, CLW_XFORM_DWT_53_A);
51-
properties.set_bits(9..13, self.entropy_algorithm.to_u16().unwrap());
51+
properties.set_bits(9..13, self.entropy_algorithm.as_u16());
5252
properties.set_bits(13..15, SCALAR_QUANTIZATION);
5353
properties.set_bit(15, false); // reserved
5454
dst.write_u16(properties);
@@ -297,7 +297,7 @@ impl Encode for TileSetPdu<'_> {
297297
properties.set_bits(1..4, OperatingMode::empty().bits()); // The decoder MUST ignore this flag
298298
properties.set_bits(4..6, COLOR_CONVERSION_ICT);
299299
properties.set_bits(6..10, CLW_XFORM_DWT_53_A);
300-
properties.set_bits(10..14, self.entropy_algorithm.to_u16().unwrap());
300+
properties.set_bits(10..14, self.entropy_algorithm.as_u16());
301301
properties.set_bits(14..16, SCALAR_QUANTIZATION);
302302
dst.write_u16(properties);
303303

@@ -666,13 +666,23 @@ impl<'de> Decode<'de> for Tile<'de> {
666666
}
667667
}
668668

669-
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
669+
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive)]
670670
#[repr(u16)]
671671
pub enum EntropyAlgorithm {
672672
Rlgr1 = 0x01,
673673
Rlgr3 = 0x04,
674674
}
675675

676+
impl EntropyAlgorithm {
677+
#[expect(
678+
clippy::as_conversions,
679+
reason = "guarantees discriminant layout, and as is the only way to cast enum -> primitive"
680+
)]
681+
fn as_u16(self) -> u16 {
682+
self as u16
683+
}
684+
}
685+
676686
bitflags! {
677687
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
678688
pub struct OperatingMode: u16 {

0 commit comments

Comments
 (0)