Skip to content

Commit

Permalink
🏷️ zv: Implement Clone for *Value
Browse files Browse the repository at this point in the history
While `try_clone` should generally be used, there are times when `Clone`
is really needed. Given that the chances of `try_clone` failing are
pretty low these days, let's implement `Clone` based on `try_clone` than
panics on `try_clone` failure.

Fixes #1078.
  • Loading branch information
zeenix committed Nov 2, 2024
1 parent 7ab9aa9 commit bcfe9ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions zvariant/src/owned_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ impl<'de> Deserialize<'de> for OwnedValue {
}
}

impl Clone for OwnedValue {
/// Clone the value.
///
/// # Panics
///
/// This method can only fail on Unix platforms for [`Value::Fd`] variant containing an
/// [`Fd::Owned`] variant. This happens when the current process exceeds the limit on maximum
/// number of open file descriptors.
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, error::Error};
Expand Down
14 changes: 14 additions & 0 deletions zvariant/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,20 @@ impl<'a> TryFrom<&Value<'a>> for Value<'a> {
}
}

impl Clone for Value<'_> {
/// Clone the value.
///
/// # Panics
///
/// This method can only fail on Unix platforms for [`Value::Fd`] variant containing an
/// [`Fd::Owned`] variant. This happens when the current process exceeds the limit on maximum
/// number of open file descriptors.
fn clone(&self) -> Self {
self.try_clone()
.expect("Process exceeded limit on maximum number of open file descriptors")
}
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
Expand Down

0 comments on commit bcfe9ed

Please sign in to comment.