Skip to content

Commit 07058ec

Browse files
committed
Clone for Derivation
And `as_ptr` too. This will be tested once we get the harmonia bindings in.
1 parent 5cf7329 commit 07058ec

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-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 {

0 commit comments

Comments
 (0)