From 76b0518afade21676139397f5a5d6a44be916dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 20 Aug 2024 18:04:40 +0400 Subject: [PATCH] refactor(core): move IntoOwned MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau --- .../src/windows/cliprdr_backend.rs | 2 +- .../src/pdu/client_temporary_directory.rs | 10 +++--- .../ironrdp-cliprdr/src/pdu/file_contents.rs | 10 +++--- .../src/pdu/format_data/mod.rs | 13 +++----- crates/ironrdp-cliprdr/src/pdu/format_list.rs | 9 +++--- crates/ironrdp-core/src/into_owned.rs | 8 +++++ crates/ironrdp-core/src/lib.rs | 2 ++ crates/ironrdp-pdu/src/lib.rs | 7 ---- crates/ironrdp-pdu/src/macros.rs | 6 ++-- crates/ironrdp-pdu/src/mcs.rs | 32 +++++++++---------- crates/ironrdp-pdu/src/x224.rs | 8 ++--- crates/ironrdp-web/src/clipboard/mod.rs | 2 +- 12 files changed, 51 insertions(+), 58 deletions(-) create mode 100644 crates/ironrdp-core/src/into_owned.rs diff --git a/crates/ironrdp-cliprdr-native/src/windows/cliprdr_backend.rs b/crates/ironrdp-cliprdr-native/src/windows/cliprdr_backend.rs index 1a34ab16..7660cfa4 100644 --- a/crates/ironrdp-cliprdr-native/src/windows/cliprdr_backend.rs +++ b/crates/ironrdp-cliprdr-native/src/windows/cliprdr_backend.rs @@ -5,7 +5,7 @@ use ironrdp_cliprdr::pdu::{ ClipboardFormat, ClipboardGeneralCapabilityFlags, FileContentsRequest, FileContentsResponse, FormatDataRequest, FormatDataResponse, LockDataId, }; -use ironrdp_core::impl_as_any; +use ironrdp_core::{impl_as_any, IntoOwned}; use windows::Win32::Foundation::{HWND, LPARAM, WPARAM}; use windows::Win32::UI::WindowsAndMessaging::PostMessageW; diff --git a/crates/ironrdp-cliprdr/src/pdu/client_temporary_directory.rs b/crates/ironrdp-cliprdr/src/pdu/client_temporary_directory.rs index 4041e774..5a2eff42 100644 --- a/crates/ironrdp-cliprdr/src/pdu/client_temporary_directory.rs +++ b/crates/ironrdp-cliprdr/src/pdu/client_temporary_directory.rs @@ -1,10 +1,8 @@ use std::borrow::Cow; -use ironrdp_core::{ReadCursor, WriteCursor}; +use ironrdp_core::{IntoOwned, ReadCursor, WriteCursor}; use ironrdp_pdu::utils::{read_string_from_cursor, write_string_to_cursor, CharacterSet}; -use ironrdp_pdu::{ - cast_int, ensure_size, impl_pdu_borrowing, invalid_message_err, IntoOwnedPdu, PduDecode, PduEncode, PduResult, -}; +use ironrdp_pdu::{cast_int, ensure_size, impl_pdu_borrowing, invalid_message_err, PduDecode, PduEncode, PduResult}; use crate::pdu::PartialHeader; @@ -16,10 +14,10 @@ pub struct ClientTemporaryDirectory<'a> { impl_pdu_borrowing!(ClientTemporaryDirectory<'_>, OwnedClientTemporaryDirectory); -impl IntoOwnedPdu for ClientTemporaryDirectory<'_> { +impl IntoOwned for ClientTemporaryDirectory<'_> { type Owned = OwnedClientTemporaryDirectory; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { OwnedClientTemporaryDirectory { path_buffer: Cow::Owned(self.path_buffer.into_owned()), } diff --git a/crates/ironrdp-cliprdr/src/pdu/file_contents.rs b/crates/ironrdp-cliprdr/src/pdu/file_contents.rs index 21dc13eb..f2e4e3fc 100644 --- a/crates/ironrdp-cliprdr/src/pdu/file_contents.rs +++ b/crates/ironrdp-cliprdr/src/pdu/file_contents.rs @@ -1,11 +1,9 @@ use std::borrow::Cow; use bitflags::bitflags; -use ironrdp_core::{ReadCursor, WriteCursor}; +use ironrdp_core::{IntoOwned, ReadCursor, WriteCursor}; use ironrdp_pdu::utils::{combine_u64, split_u64}; -use ironrdp_pdu::{ - cast_int, ensure_size, impl_pdu_borrowing, invalid_message_err, IntoOwnedPdu, PduDecode, PduEncode, PduResult, -}; +use ironrdp_pdu::{cast_int, ensure_size, impl_pdu_borrowing, invalid_message_err, PduDecode, PduEncode, PduResult}; use crate::pdu::{ClipboardPduFlags, PartialHeader}; @@ -36,10 +34,10 @@ pub struct FileContentsResponse<'a> { impl_pdu_borrowing!(FileContentsResponse<'_>, OwnedFileContentsResponse); -impl IntoOwnedPdu for FileContentsResponse<'_> { +impl IntoOwned for FileContentsResponse<'_> { type Owned = OwnedFileContentsResponse; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { OwnedFileContentsResponse { is_error: self.is_error, stream_id: self.stream_id, diff --git a/crates/ironrdp-cliprdr/src/pdu/format_data/mod.rs b/crates/ironrdp-cliprdr/src/pdu/format_data/mod.rs index 78077738..564b8d08 100644 --- a/crates/ironrdp-cliprdr/src/pdu/format_data/mod.rs +++ b/crates/ironrdp-cliprdr/src/pdu/format_data/mod.rs @@ -9,11 +9,10 @@ pub use self::palette::*; #[rustfmt::skip] use std::borrow::Cow; +use ironrdp_core::IntoOwned; use ironrdp_core::{ReadCursor, WriteCursor}; use ironrdp_pdu::utils::{read_string_from_cursor, to_utf16_bytes, CharacterSet}; -use ironrdp_pdu::{ - cast_int, ensure_fixed_part_size, ensure_size, impl_pdu_borrowing, IntoOwnedPdu, PduDecode, PduEncode, PduResult, -}; +use ironrdp_pdu::{cast_int, ensure_fixed_part_size, ensure_size, impl_pdu_borrowing, PduDecode, PduEncode, PduResult}; use super::ClipboardFormatId; use crate::pdu::{ClipboardPduFlags, PartialHeader}; @@ -27,10 +26,10 @@ pub struct FormatDataResponse<'a> { impl_pdu_borrowing!(FormatDataResponse<'_>, OwnedFormatDataResponse); -impl IntoOwnedPdu for FormatDataResponse<'_> { +impl IntoOwned for FormatDataResponse<'_> { type Owned = OwnedFormatDataResponse; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { OwnedFormatDataResponse { is_error: self.is_error, data: Cow::Owned(self.data.into_owned()), @@ -163,10 +162,6 @@ impl<'a> FormatDataResponse<'a> { read_string_from_cursor(&mut cursor, CharacterSet::Unicode, true) } - pub fn into_owned(self) -> OwnedFormatDataResponse { - self.into_owned_pdu() - } - pub fn into_data(self) -> Cow<'a, [u8]> { self.data } diff --git a/crates/ironrdp-cliprdr/src/pdu/format_list.rs b/crates/ironrdp-cliprdr/src/pdu/format_list.rs index ad865c5c..4b7a35b4 100644 --- a/crates/ironrdp-cliprdr/src/pdu/format_list.rs +++ b/crates/ironrdp-cliprdr/src/pdu/format_list.rs @@ -1,10 +1,9 @@ use std::borrow::Cow; -use ironrdp_core::{ReadCursor, WriteCursor}; +use ironrdp_core::{IntoOwned, ReadCursor, WriteCursor}; use ironrdp_pdu::utils::{read_string_from_cursor, to_utf16_bytes, write_string_to_cursor, CharacterSet}; use ironrdp_pdu::{ - cast_int, ensure_size, impl_pdu_borrowing, impl_pdu_pod, invalid_message_err, IntoOwnedPdu, PduDecode, PduEncode, - PduResult, + cast_int, ensure_size, impl_pdu_borrowing, impl_pdu_pod, invalid_message_err, PduDecode, PduEncode, PduResult, }; use crate::pdu::{ClipboardPduFlags, PartialHeader}; @@ -219,10 +218,10 @@ pub struct FormatList<'a> { impl_pdu_borrowing!(FormatList<'_>, OwnedFormatList); -impl IntoOwnedPdu for FormatList<'_> { +impl IntoOwned for FormatList<'_> { type Owned = OwnedFormatList; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { OwnedFormatList { use_ascii: self.use_ascii, encoded_formats: Cow::Owned(self.encoded_formats.into_owned()), diff --git a/crates/ironrdp-core/src/into_owned.rs b/crates/ironrdp-core/src/into_owned.rs new file mode 100644 index 00000000..cf451fc3 --- /dev/null +++ b/crates/ironrdp-core/src/into_owned.rs @@ -0,0 +1,8 @@ +/// Used to produce an owned version of a given data. +pub trait IntoOwned: Sized { + /// The resulting type after obtaining ownership. + type Owned: 'static; + + /// Creates owned data from data. + fn into_owned(self) -> Self::Owned; +} diff --git a/crates/ironrdp-core/src/lib.rs b/crates/ironrdp-core/src/lib.rs index 424f6463..45142e86 100644 --- a/crates/ironrdp-core/src/lib.rs +++ b/crates/ironrdp-core/src/lib.rs @@ -12,11 +12,13 @@ mod macros; mod as_any; mod cursor; +mod into_owned; // Flat API hierarchy of common traits and types pub use self::as_any::*; pub use self::cursor::*; +pub use self::into_owned::*; // Trait that can only be implemented within the current module pub(crate) mod private { diff --git a/crates/ironrdp-pdu/src/lib.rs b/crates/ironrdp-pdu/src/lib.rs index 6901dd11..062c801b 100644 --- a/crates/ironrdp-pdu/src/lib.rs +++ b/crates/ironrdp-pdu/src/lib.rs @@ -247,13 +247,6 @@ pub fn decode_owned_cursor(src: &mut ReadCursor<'_>) -> PduRe T::decode_owned(src) } -/// Trait used to produce an owned version of a given PDU. -pub trait IntoOwnedPdu: Sized { - type Owned: 'static; - - fn into_owned_pdu(self) -> Self::Owned; -} - #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[repr(u8)] pub enum Action { diff --git a/crates/ironrdp-pdu/src/macros.rs b/crates/ironrdp-pdu/src/macros.rs index c74b3799..09a982b7 100644 --- a/crates/ironrdp-pdu/src/macros.rs +++ b/crates/ironrdp-pdu/src/macros.rs @@ -223,10 +223,10 @@ macro_rules! const_assert { #[macro_export] macro_rules! impl_pdu_pod { ($pdu_ty:ty) => { - impl $crate::IntoOwnedPdu for $pdu_ty { + impl ::ironrdp_core::IntoOwned for $pdu_ty { type Owned = Self; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { self } } @@ -248,7 +248,7 @@ macro_rules! impl_pdu_borrowing { impl $crate::PduDecodeOwned for $owned_ty { fn decode_owned(src: &mut ReadCursor<'_>) -> $crate::PduResult { let pdu = <$pdu_ty $(<$($lt),+>)? as $crate::PduDecode>::decode(src)?; - Ok($crate::IntoOwnedPdu::into_owned_pdu(pdu)) + Ok(ironrdp_core::IntoOwned::into_owned(pdu)) } } }; diff --git a/crates/ironrdp-pdu/src/mcs.rs b/crates/ironrdp-pdu/src/mcs.rs index 3af48b46..b5056d51 100644 --- a/crates/ironrdp-pdu/src/mcs.rs +++ b/crates/ironrdp-pdu/src/mcs.rs @@ -4,8 +4,8 @@ use crate::gcc::{ChannelDef, ClientGccBlocks, ConferenceCreateRequest, Conferenc use crate::tpdu::{TpduCode, TpduHeader}; use crate::tpkt::TpktHeader; use crate::x224::{user_data_size, X224Pdu}; -use crate::{per, IntoOwnedPdu, PduError, PduErrorExt as _, PduResult}; -use ironrdp_core::{ReadCursor, WriteCursor}; +use crate::{per, PduError, PduErrorExt as _, PduResult}; +use ironrdp_core::{IntoOwned, ReadCursor, WriteCursor}; // T.125 MCS is defined in: // @@ -274,19 +274,19 @@ pub enum McsMessage<'a> { impl_pdu_borrowing!(McsMessage<'_>, OwnedMcsMessage); -impl IntoOwnedPdu for McsMessage<'_> { +impl IntoOwned for McsMessage<'_> { type Owned = OwnedMcsMessage; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { match self { - Self::ErectDomainRequest(msg) => McsMessage::ErectDomainRequest(msg.into_owned_pdu()), - Self::AttachUserRequest(msg) => McsMessage::AttachUserRequest(msg.into_owned_pdu()), - Self::AttachUserConfirm(msg) => McsMessage::AttachUserConfirm(msg.into_owned_pdu()), - Self::ChannelJoinRequest(msg) => McsMessage::ChannelJoinRequest(msg.into_owned_pdu()), - Self::ChannelJoinConfirm(msg) => McsMessage::ChannelJoinConfirm(msg.into_owned_pdu()), - Self::SendDataRequest(msg) => McsMessage::SendDataRequest(msg.into_owned_pdu()), - Self::SendDataIndication(msg) => McsMessage::SendDataIndication(msg.into_owned_pdu()), - Self::DisconnectProviderUltimatum(msg) => McsMessage::DisconnectProviderUltimatum(msg.into_owned_pdu()), + Self::ErectDomainRequest(msg) => McsMessage::ErectDomainRequest(msg.into_owned()), + Self::AttachUserRequest(msg) => McsMessage::AttachUserRequest(msg.into_owned()), + Self::AttachUserConfirm(msg) => McsMessage::AttachUserConfirm(msg.into_owned()), + Self::ChannelJoinRequest(msg) => McsMessage::ChannelJoinRequest(msg.into_owned()), + Self::ChannelJoinConfirm(msg) => McsMessage::ChannelJoinConfirm(msg.into_owned()), + Self::SendDataRequest(msg) => McsMessage::SendDataRequest(msg.into_owned()), + Self::SendDataIndication(msg) => McsMessage::SendDataIndication(msg.into_owned()), + Self::DisconnectProviderUltimatum(msg) => McsMessage::DisconnectProviderUltimatum(msg.into_owned()), } } } @@ -557,10 +557,10 @@ pub struct SendDataRequest<'a> { impl_pdu_borrowing!(SendDataRequest<'_>, OwnedSendDataRequest); -impl IntoOwnedPdu for SendDataRequest<'_> { +impl IntoOwned for SendDataRequest<'_> { type Owned = OwnedSendDataRequest; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { SendDataRequest { user_data: Cow::Owned(self.user_data.into_owned()), ..self @@ -638,10 +638,10 @@ pub struct SendDataIndication<'a> { impl_pdu_borrowing!(SendDataIndication<'_>, OwnedSendDataIndication); -impl IntoOwnedPdu for SendDataIndication<'_> { +impl IntoOwned for SendDataIndication<'_> { type Owned = OwnedSendDataIndication; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { SendDataIndication { user_data: Cow::Owned(self.user_data.into_owned()), ..self diff --git a/crates/ironrdp-pdu/src/x224.rs b/crates/ironrdp-pdu/src/x224.rs index 09233358..b84a76da 100644 --- a/crates/ironrdp-pdu/src/x224.rs +++ b/crates/ironrdp-pdu/src/x224.rs @@ -2,8 +2,8 @@ use std::borrow::Cow; use crate::tpdu::{TpduCode, TpduHeader}; use crate::tpkt::TpktHeader; -use crate::{IntoOwnedPdu, Pdu, PduDecode, PduEncode, PduError, PduErrorExt as _, PduResult}; -use ironrdp_core::{ReadCursor, WriteCursor}; +use crate::{Pdu, PduDecode, PduEncode, PduError, PduErrorExt as _, PduResult}; +use ironrdp_core::{IntoOwned, ReadCursor, WriteCursor}; pub trait X224Pdu<'de>: Sized { const X224_NAME: &'static str; @@ -92,10 +92,10 @@ pub struct X224Data<'a> { impl_pdu_borrowing!(X224Data<'_>, OwnedX224Data); -impl IntoOwnedPdu for X224Data<'_> { +impl IntoOwned for X224Data<'_> { type Owned = OwnedX224Data; - fn into_owned_pdu(self) -> Self::Owned { + fn into_owned(self) -> Self::Owned { X224Data { data: Cow::Owned(self.data.into_owned()), } diff --git a/crates/ironrdp-web/src/clipboard/mod.rs b/crates/ironrdp-web/src/clipboard/mod.rs index b300cd08..704d3468 100644 --- a/crates/ironrdp-web/src/clipboard/mod.rs +++ b/crates/ironrdp-web/src/clipboard/mod.rs @@ -23,7 +23,7 @@ use ironrdp::cliprdr::pdu::{ }; use ironrdp_cliprdr_format::bitmap::{dib_to_png, dibv5_to_png, png_to_cf_dibv5}; use ironrdp_cliprdr_format::html::{cf_html_to_plain_html, plain_html_to_cf_html}; -use ironrdp_core::impl_as_any; +use ironrdp_core::{impl_as_any, IntoOwned}; use transaction::{ClipboardContent, ClipboardContentValue}; use wasm_bindgen::prelude::*;