public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH pathpatterns 1/2] match_list: remove unsafe std::mem::transmute
Date: Mon, 18 Sep 2023 15:41:08 +0200	[thread overview]
Message-ID: <20230918134109.179258-2-g.goller@proxmox.com> (raw)
In-Reply-To: <20230918134109.179258-1-g.goller@proxmox.com>

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 <g.goller@proxmox.com>
---
 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<P, U>(&self, path: P, get_file_mode: U) -> Result<Option<MatchType>, U::Error>
+    fn matches<P, U>(&'a self, path: P, get_file_mode: U) -> Result<Option<MatchType>, 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<P, U>(&self, path: P, get_file_mode: U) -> Result<Option<MatchType>, U::Error>
+    fn matches_exact<P, U>(
+        &'a self,
+        path: P,
+        get_file_mode: U,
+    ) -> Result<Option<MatchType>, 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<P, G>(&self, path: P, get_file_mode: G) -> Result<Option<MatchType>, G::Error>
+    fn matches<P, G>(&'a self, path: P, get_file_mode: G) -> Result<Option<MatchType>, 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<P, G>(&self, path: P, get_file_mode: G) -> Result<Option<MatchType>, G::Error>
+    fn matches_exact<P, G>(
+        &'a self,
+        path: P,
+        get_file_mode: G,
+    ) -> Result<Option<MatchType>, 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





  reply	other threads:[~2023-09-18 13:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-18 13:41 [pbs-devel] [PATCH pathpatterns 0/2] " Gabriel Goller
2023-09-18 13:41 ` Gabriel Goller [this message]
2023-09-18 13:41 ` [pbs-devel] [PATCH proxmox-backup 2/2] datastore: catalog: added lifetime to find function Gabriel Goller
2023-10-11 11:07   ` Gabriel Goller
2023-10-19  9:16 ` [pbs-devel] applied: [PATCH pathpatterns 0/2] remove unsafe std::mem::transmute Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230918134109.179258-2-g.goller@proxmox.com \
    --to=g.goller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal