Skip to content

Commit df96db5

Browse files
committed
Add wrapper for configuration set/get
1 parent 6c7e005 commit df96db5

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

netcdf-sys/build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ fn main() {
306306
"cargo:rustc-cfg=feature=\"{}.{}.{}\"",
307307
version.major, version.minor, version.patch
308308
);
309+
println!(
310+
"cargo:version_\"{}.{}.{}\"=1",
311+
version.major, version.minor, version.patch
312+
);
309313
}
310314
}
311315
metaheader.emit_feature_flags();

netcdf/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static = ["netcdf-sys/static"]
2323
ndarray = { version = "0.15", optional = true }
2424
netcdf-sys = { workspace = true }
2525
bitflags = "2.4.2"
26+
libc = "0.2.154"
2627

2728
[dev-dependencies]
2829
clap = { version = "4.5.1", features = ["derive"] }

netcdf/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ fn main() {
22
if std::env::var("DEP_NETCDF_HAS_MMAP").is_ok() {
33
println!("cargo:rustc-cfg=feature=\"has-mmap\"");
44
}
5+
for (env, _value) in std::env::vars() {
6+
if let Some(version) = env.strip_prefix("DEP_NETCDF_VERSION_") {
7+
println!("cargo:rustc-cfg=feature={version}");
8+
}
9+
}
510
}

netcdf/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pub(crate) mod error;
110110
pub(crate) mod extent;
111111
pub(crate) mod file;
112112
pub(crate) mod group;
113+
#[cfg(feature = "4.9.2")]
114+
pub mod rc;
113115
pub mod types;
114116
pub(crate) mod variable;
115117

netcdf/src/rc.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::ffi::{c_char, CStr, CString};
2+
use std::ops::Deref;
3+
use std::ptr::NonNull;
4+
5+
pub fn set(key: &str, value: &str) -> crate::error::Result<()> {
6+
let key = CString::new(key)?;
7+
let value = CString::new(value)?;
8+
crate::error::checked(crate::with_lock(|| unsafe {
9+
netcdf_sys::nc_rc_set(key.as_ptr(), value.as_ptr())
10+
}))
11+
}
12+
13+
#[derive(Debug)]
14+
pub struct OwnedString {
15+
inner: NonNull<c_char>,
16+
}
17+
18+
impl Deref for OwnedString {
19+
type Target = CStr;
20+
fn deref(&self) -> &Self::Target {
21+
unsafe { CStr::from_ptr(self.inner.as_ptr()) }
22+
}
23+
}
24+
25+
impl Drop for OwnedString {
26+
fn drop(&mut self) {
27+
unsafe {
28+
libc::free(self.inner.as_ptr().cast());
29+
}
30+
}
31+
}
32+
33+
pub fn get(key: &str) -> Option<OwnedString> {
34+
let key = if let Ok(key) = CString::new(key) {
35+
key
36+
} else {
37+
return None;
38+
};
39+
let _lock = netcdf_sys::libnetcdf_lock.lock().unwrap();
40+
let value = unsafe { netcdf_sys::nc_rc_get(key.as_ptr()) };
41+
NonNull::new(value).map(|inner| OwnedString { inner })
42+
}

0 commit comments

Comments
 (0)