2024-12-16 14:11:17 +01:00
|
|
|
use std::ptr::NonNull;
|
|
|
|
|
|
2024-04-08 16:54:36 +02:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use nix_c_raw as raw;
|
2024-05-22 14:45:12 +02:00
|
|
|
use nix_util::{
|
|
|
|
|
result_string_init,
|
|
|
|
|
string_return::{callback_get_result_string, callback_get_result_string_data},
|
|
|
|
|
};
|
2024-04-08 16:54:36 +02:00
|
|
|
|
|
|
|
|
pub struct StorePath {
|
2024-12-16 14:11:17 +01:00
|
|
|
raw: NonNull<raw::StorePath>,
|
2024-04-08 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
impl StorePath {
|
2024-05-24 11:15:43 +02:00
|
|
|
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
|
|
|
|
|
///
|
|
|
|
|
/// Construct a new `StorePath` by first cloning the C store path.
|
|
|
|
|
/// This does not take ownership of the C store path, so it should be a borrowed value, or you should free it.
|
2024-12-16 14:11:17 +01:00
|
|
|
pub fn new_raw_clone(raw: NonNull<raw::StorePath>) -> Self {
|
|
|
|
|
Self::new_raw(
|
|
|
|
|
NonNull::new(unsafe { raw::store_path_clone(raw.as_ptr()) })
|
|
|
|
|
.or_else(|| panic!("nix_store_path_clone returned a null pointer"))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
2024-04-08 16:54:36 +02:00
|
|
|
}
|
2024-05-24 11:15:43 +02:00
|
|
|
/// This is a low level function that you shouldn't have to call unless you are developing the Nix bindings.
|
|
|
|
|
///
|
|
|
|
|
/// Takes ownership of a C `nix_store_path`. It will be freed when the `StorePath` is dropped.
|
2024-12-16 14:11:17 +01:00
|
|
|
pub fn new_raw(raw: NonNull<raw::StorePath>) -> Self {
|
2024-04-08 16:54:36 +02:00
|
|
|
StorePath { raw }
|
|
|
|
|
}
|
|
|
|
|
pub fn name(&self) -> Result<String> {
|
|
|
|
|
unsafe {
|
2024-05-22 14:45:12 +02:00
|
|
|
let mut r = result_string_init!();
|
2024-04-08 16:54:36 +02:00
|
|
|
raw::store_path_name(
|
2024-12-16 14:11:17 +01:00
|
|
|
self.as_ptr(),
|
2024-05-22 14:45:12 +02:00
|
|
|
Some(callback_get_result_string),
|
|
|
|
|
callback_get_result_string_data(&mut r),
|
2024-04-08 16:54:36 +02:00
|
|
|
);
|
2024-05-22 14:45:12 +02:00
|
|
|
r
|
2024-04-08 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-16 14:11:17 +01:00
|
|
|
|
|
|
|
|
pub fn as_ptr(&self) -> *mut nix_c_raw::StorePath {
|
|
|
|
|
self.raw.as_ptr()
|
|
|
|
|
}
|
2024-04-08 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
impl Drop for StorePath {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
2024-12-16 14:11:17 +01:00
|
|
|
raw::store_path_free(self.as_ptr());
|
2024-04-08 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|