| Crates.io | apr |
| lib.rs | apr |
| version | 0.3.2 |
| created_at | 2023-10-31 22:23:56.144316+00 |
| updated_at | 2025-09-08 00:56:32.820911+00 |
| description | Rust bindings for Apache Portable Runtime |
| homepage | https://github.com/jelmer/apr-rs |
| repository | https://github.com/jelmer/apr-rs.git |
| max_upload_size | |
| id | 1020430 |
| size | 301,489 |
Rust bindings for the Apache Portable Runtime (APR) library and the associated APR-Util library.
This crate provides safe Rust bindings to the Apache Portable Runtime (APR), a C library that forms the foundation of the Apache HTTP Server and other Apache projects. APR provides a predictable and consistent interface to underlying platform-specific implementations for:
This crate is primarily useful when developing Rust bindings for C libraries that depend on APR. Many Apache projects and other C libraries use APR for cross-platform compatibility and memory management. If you're creating Rust bindings for such libraries, this crate provides the necessary APR functionality with a safe Rust interface.
Add this to your Cargo.toml:
[dependencies]
apr = "0.3"
You need to have APR and APR-Util installed on your system:
sudo apt-get install libapr1-dev libaprutil1-dev
sudo dnf install apr-devel apr-util-devel
brew install apr apr-util
If you need to build APR from source, download it from the Apache APR website.
APR uses memory pools for all memory allocation. This is fundamental when working with APR-based C libraries:
use apr::Pool;
fn main() -> apr::Result<()> {
// Create a root memory pool
let pool = Pool::new();
// Pools can have child pools for hierarchical memory management
let subpool = pool.create_subpool()?;
// Memory allocated from pools is automatically freed when the pool is dropped
Ok(())
}
When creating bindings for C libraries that use APR, you'll typically need to:
Example of integrating with a hypothetical APR-based C library:
use apr::{Pool, Status};
use std::ptr;
// Hypothetical C library that uses APR
extern "C" {
fn some_c_function(pool: *mut apr_sys::apr_pool_t) -> apr_sys::apr_status_t;
}
fn main() -> apr::Result<()> {
let pool = Pool::new();
// Get raw APR pool pointer to pass to C functions
let status = unsafe {
Status::from(some_c_function(pool.as_mut_ptr()))
};
if status.is_success() {
println!("C function succeeded!");
} else {
return Err(status.into());
}
Ok(())
}
use apr::{Pool, file::File};
fn main() -> apr::Result<()> {
let pool = Pool::new();
// Open a file using APR
let file = File::open("example.txt", apr::file::Flag::READ, 0, &pool)?;
// Read file contents
let mut buffer = vec![0u8; 1024];
let bytes_read = file.read(&mut buffer)?;
println!("Read {} bytes", bytes_read);
Ok(())
}
use apr::{Pool, hash::Hash};
fn main() -> apr::Result<()> {
let pool = Pool::new();
// Create a hash table
let mut hash = Hash::<String>::new(&pool);
// Insert key-value pairs
hash.set("key1", "value1".to_string());
hash.set("key2", "value2".to_string());
// Retrieve values
if let Some(value) = hash.get("key1") {
println!("Found: {}", value);
}
Ok(())
}
The crate is organized into modules that mirror APR's structure:
pool - Memory pool managementfile - File I/O operationsnetwork - Network I/O and socket operationshash - Hash table implementationtables - APR table (ordered key-value pairs)strings - String manipulation utilitiestime - Time handling functionserror - Error handling and status codescrypto - Cryptographic functions (MD5, SHA1, etc.)base64 - Base64 encoding/decodinguri - URI parsing and manipulationuuid - UUID generationxml - XML parsing utilitiesThis crate aims to provide safe Rust abstractions over APR's C API. However, when interfacing with C libraries:
unsafe blocks when dealing with raw pointersContributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
When contributing, please:
cargo testThis project is licensed under the Apache License 2.0 - see the LICENSE file for details.
For questions and discussions, please use the GitHub issues tracker.