From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id E6ACDCA6C for ; Mon, 18 Sep 2023 15:41:45 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C981D24E for ; Mon, 18 Sep 2023 15:41:15 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 18 Sep 2023 15:41:14 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5493847F46 for ; Mon, 18 Sep 2023 15:41:14 +0200 (CEST) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Mon, 18 Sep 2023 15:41:08 +0200 Message-Id: <20230918134109.179258-2-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918134109.179258-1-g.goller@proxmox.com> References: <20230918134109.179258-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.385 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH pathpatterns 1/2] match_list: remove unsafe std::mem::transmute X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Sep 2023 13:41:45 -0000 By adding a lifetime to the `MatchList` trait, we don't need to use unsafe code to transmute self anymore. Signed-off-by: Gabriel Goller --- src/match_list.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/match_list.rs b/src/match_list.rs index 8a2ac02..98b0d59 100644 --- a/src/match_list.rs +++ b/src/match_list.rs @@ -412,41 +412,42 @@ impl MatchListEntry for &'_ &'_ MatchEntry { /// [`VecDeque`](std::collections::VecDeque) etc. /// /// This makes it easier to use slices over entries or references to entries. -pub trait MatchList { +pub trait MatchList<'a> { /// Check whether this list contains anything matching a prefix of the specified path, and the /// specified file mode. Gets the file_mode lazily, only if needed. - fn matches(&self, path: P, get_file_mode: U) -> Result, U::Error> + fn matches(&'a self, path: P, get_file_mode: U) -> Result, U::Error> where P: AsRef<[u8]>, U: GetFileMode; /// Check whether this list contains anything exactly matching the path and mode. Gets the /// file_mode lazily, only if needed. - fn matches_exact(&self, path: P, get_file_mode: U) -> Result, U::Error> + fn matches_exact( + &'a self, + path: P, + get_file_mode: U, + ) -> Result, U::Error> where P: AsRef<[u8]>, U: GetFileMode; } -impl<'a, T> MatchList for T +impl<'a, T> MatchList<'a> for T where T: 'a + ?Sized, &'a T: IntoIterator, <&'a T as IntoIterator>::IntoIter: DoubleEndedIterator, <&'a T as IntoIterator>::Item: MatchListEntry, { - fn matches(&self, path: P, get_file_mode: G) -> Result, G::Error> + fn matches(&'a self, path: P, get_file_mode: G) -> Result, G::Error> where P: AsRef<[u8]>, G: GetFileMode, { - // This is an &self method on a `T where T: 'a`. - let this: &'a Self = unsafe { std::mem::transmute(self) }; - let mut get_file_mode = Some(get_file_mode); let mut file_mode = None; - for m in this.into_iter().rev() { + for m in self.into_iter().rev() { if file_mode.is_none() && m.entry_needs_file_mode() { file_mode = Some(get_file_mode.take().unwrap().get()?); } @@ -457,18 +458,19 @@ where Ok(None) } - fn matches_exact(&self, path: P, get_file_mode: G) -> Result, G::Error> + fn matches_exact( + &'a self, + path: P, + get_file_mode: G, + ) -> Result, G::Error> where P: AsRef<[u8]>, G: GetFileMode, { - // This is an &self method on a `T where T: 'a`. - let this: &'a Self = unsafe { std::mem::transmute(self) }; - let mut get_file_mode = Some(get_file_mode); let mut file_mode = None; - for m in this.into_iter().rev() { + for m in self.into_iter().rev() { if file_mode.is_none() && m.entry_needs_file_mode() { file_mode = Some(get_file_mode.take().unwrap().get()?); } -- 2.39.2