Skip to content

Commit 17f0e65

Browse files
authored
Merge pull request #25 from nixops4/clone
Clone
2 parents f24b749 + 07058ec commit 17f0e65

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

nix-bindings-store/src/derivation.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@ impl Derivation {
1414
pub(crate) fn new_raw(inner: NonNull<raw::derivation>) -> Self {
1515
Derivation { inner }
1616
}
17+
18+
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
19+
///
20+
/// Construct a new `Derivation` by first cloning the C derivation.
21+
///
22+
/// # Safety
23+
///
24+
/// This does not take ownership of the C derivation, so it should be a borrowed pointer, or you should free it.
25+
pub unsafe fn new_raw_clone(inner: NonNull<raw::derivation>) -> Self {
26+
Self::new_raw(
27+
NonNull::new(raw::derivation_clone(inner.as_ptr()))
28+
.or_else(|| panic!("nix_derivation_clone returned a null pointer"))
29+
.unwrap(),
30+
)
31+
}
32+
33+
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
34+
///
35+
/// Get a pointer to the underlying Nix C API derivation.
36+
///
37+
/// # Safety
38+
///
39+
/// This function is unsafe because it returns a raw pointer. The caller must ensure that the pointer is not used beyond the lifetime of this `Derivation`.
40+
pub unsafe fn as_ptr(&self) -> *mut raw::derivation {
41+
self.inner.as_ptr()
42+
}
43+
}
44+
45+
impl Clone for Derivation {
46+
fn clone(&self) -> Self {
47+
unsafe { Self::new_raw_clone(self.inner) }
48+
}
1749
}
1850

1951
impl Drop for Derivation {

nix-bindings-store/src/path.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ impl StorePath {
6464
self.raw.as_ptr()
6565
}
6666
}
67+
68+
impl Clone for StorePath {
69+
fn clone(&self) -> Self {
70+
unsafe { Self::new_raw_clone(self.raw) }
71+
}
72+
}
73+
6774
impl Drop for StorePath {
6875
fn drop(&mut self) {
6976
unsafe {

0 commit comments

Comments
 (0)