1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
//! Basic usage examples for the apr-rs crate.
//!
//! This example demonstrates fundamental APR concepts that are essential
//! when working with APR-based C libraries.
use apr::hash::{Hash, TypedHash};
use apr::tables::StringTable;
use apr::{Pool, Result, Status};
use std::ffi::c_void;
fn main() -> Result<()> {
// Memory Pools - The foundation of APR memory management
let root_pool = Pool::new();
let subpool = Pool::new();
let _pool_ptr = root_pool.as_mut_ptr(); // Raw pointer for C interop
drop(subpool); // Subpool cleaned up
// Hash Tables - using TypedHash for string keys and values
let config_path = "/etc/myapp.conf".to_string();
let log_level = "debug".to_string();
let port = "8080".to_string();
let mut hash = TypedHash::new(&root_pool);
hash.insert_ref("config_path", &config_path);
hash.insert_ref("log_level", &log_level);
hash.insert_ref("port", &port);
if let Some(path) = hash.get_ref("config_path") {
println!("Config path: {}", path);
}
// Raw Hash example - stores raw pointers
let mut raw_hash = Hash::new(&root_pool);
unsafe {
raw_hash.insert(b"key1", &config_path as *const _ as *mut c_void);
if let Some(ptr) = raw_hash.get(b"key1") {
let value = &*(ptr as *const String);
println!("Raw hash value: {}", value);
}
}
// APR Tables (allows duplicate keys) - using StringTable for convenience
let mut table = StringTable::new(&root_pool, 10);
table.set("Content-Type", "text/html");
table.add("Cache-Control", "no-cache");
if let Some(content_type) = table.get("Content-Type") {
println!("Content-Type: {}", content_type);
}
// Error Handling
let error_status = Status::from(apr_sys::APR_ENOENT as i32);
if !error_status.is_success() {
println!("Error: {}", apr::Error::from(error_status));
}
Ok(())
}
|