* [pbs-devel] [PATCH pathpatterns 1/2] match_list: remove unsafe std::mem::transmute
2023-09-18 13:41 [pbs-devel] [PATCH pathpatterns 0/2] remove unsafe std::mem::transmute Gabriel Goller
@ 2023-09-18 13:41 ` Gabriel Goller
2023-09-18 13:41 ` [pbs-devel] [PATCH proxmox-backup 2/2] datastore: catalog: added lifetime to find function Gabriel Goller
2023-10-19 9:16 ` [pbs-devel] applied: [PATCH pathpatterns 0/2] remove unsafe std::mem::transmute Wolfgang Bumiller
2 siblings, 0 replies; 5+ messages in thread
From: Gabriel Goller @ 2023-09-18 13:41 UTC (permalink / raw)
To: pbs-devel
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [pbs-devel] [PATCH proxmox-backup 2/2] datastore: catalog: added lifetime to find function
2023-09-18 13:41 [pbs-devel] [PATCH pathpatterns 0/2] remove unsafe std::mem::transmute Gabriel Goller
2023-09-18 13:41 ` [pbs-devel] [PATCH pathpatterns 1/2] match_list: " Gabriel Goller
@ 2023-09-18 13:41 ` 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
2 siblings, 1 reply; 5+ messages in thread
From: Gabriel Goller @ 2023-09-18 13:41 UTC (permalink / raw)
To: pbs-devel
Added lifetime to `find` function. We need this lifetime
because of the `impl MatchList` and 'anonymous lifetimes in
`impl Trait` are unstable'.
Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
pbs-datastore/src/catalog.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pbs-datastore/src/catalog.rs b/pbs-datastore/src/catalog.rs
index 86e20c92..eb531837 100644
--- a/pbs-datastore/src/catalog.rs
+++ b/pbs-datastore/src/catalog.rs
@@ -661,11 +661,11 @@ impl<R: Read + Seek> CatalogReader<R> {
/// Finds all entries matching the given match patterns and calls the
/// provided callback on them.
- pub fn find(
+ pub fn find<'a>(
&mut self,
parent: &DirEntry,
file_path: &mut Vec<u8>,
- match_list: &impl MatchList, //&[MatchEntry],
+ match_list: &'a impl MatchList<'a>, //&[MatchEntry],
callback: &mut dyn FnMut(&[u8]) -> Result<(), Error>,
) -> Result<(), Error> {
let file_len = file_path.len();
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread