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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
# apr-rs
Rust bindings for the Apache Portable Runtime (APR) library and the associated APR-Util library.
[](https://crates.io/crates/apr)
[](https://docs.rs/apr)
[](LICENSE)
## Overview
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:
- Memory management and pool allocation
- File and network I/O
- Process and thread management
- Time handling
- String manipulation
- Data structures (hash tables, arrays, etc.)
- Cryptographic functions
## Primary Use Case: C Library Interoperability
**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.
### Examples of C libraries that use APR:
- Apache HTTP Server modules
- Subversion (SVN) libraries
- Apache Serf
- Any custom C library built on top of APR
## Features
- **Safe Rust API**: Wraps APR's C API with safe Rust abstractions
- **Memory pools**: APR's hierarchical memory management system
- **Cross-platform**: Inherits APR's platform abstraction layer
- **Comprehensive coverage**: Bindings for most commonly-used APR and APR-Util functionality
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
apr = "0.3"
```
### Prerequisites
You need to have APR and APR-Util installed on your system:
#### Ubuntu/Debian
```bash
sudo apt-get install libapr1-dev libaprutil1-dev
```
#### Fedora/RHEL/CentOS
```bash
sudo dnf install apr-devel apr-util-devel
```
#### macOS (using Homebrew)
```bash
brew install apr apr-util
```
#### Building from source
If you need to build APR from source, download it from the [Apache APR website](https://apr.apache.org/).
## Usage Examples
### Basic Pool Usage
APR uses memory pools for all memory allocation. This is fundamental when working with APR-based C libraries:
```rust
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(())
}
```
### Working with APR-based C Libraries
When creating bindings for C libraries that use APR, you'll typically need to:
1. Initialize APR (handled automatically by this crate)
2. Create memory pools for the C library to use
3. Pass APR types between Rust and C
Example of integrating with a hypothetical APR-based C library:
```rust
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(())
}
```
### File Operations
```rust
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(())
}
```
### Hash Tables
```rust
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(())
}
```
## Module Organization
The crate is organized into modules that mirror APR's structure:
- `pool` - Memory pool management
- `file` - File I/O operations
- `network` - Network I/O and socket operations
- `hash` - Hash table implementation
- `tables` - APR table (ordered key-value pairs)
- `strings` - String manipulation utilities
- `time` - Time handling functions
- `error` - Error handling and status codes
- `crypto` - Cryptographic functions (MD5, SHA1, etc.)
- `base64` - Base64 encoding/decoding
- `uri` - URI parsing and manipulation
- `uuid` - UUID generation
- `xml` - XML parsing utilities
## Safety
This crate aims to provide safe Rust abstractions over APR's C API. However, when interfacing with C libraries:
- Some operations require `unsafe` blocks when dealing with raw pointers
- The crate handles APR initialization automatically using Rust's standard library features
- Memory management through pools helps prevent memory leaks
- Rust's ownership system is leveraged to ensure proper resource cleanup
## Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
When contributing, please:
- Add tests for new functionality
- Update documentation as needed
- Follow Rust naming conventions and idioms
- Ensure all tests pass with `cargo test`
## License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
## Related Projects
- [apr-sys](https://crates.io/crates/apr-sys) - Low-level FFI bindings to APR (used by this crate)
- [Apache APR](https://apr.apache.org/) - The underlying C library
## Support
For questions and discussions, please use the GitHub issues tracker.
|