A Rust implementation of a Shared Memory Buffer for efficient inter-process communication on Linux.
This project was inspired by the need for a faster alternative to Unix Domain Sockets and TCP for localhost communication.
This project is currently a test implementation and will be converted into a library soon.
- Rust 1.28 or later
- Linux
- Clone the repository:
git clone https://github.com/primalpimmy/charon.git
- Build the project:
cd charon cargo build --release
use charon::ShmRingBuffer;
fn main() {
// Create a new shared memory ring buffer.
let mut shm = ShmRingBuffer::new("my_shm").unwrap();
// Data to write.
let data_to_write = b"Hello, Charon!";
// Write data to the buffer.
match shm.write(data_to_write) {
Ok(bytes_written) => {
println!("Wrote {} bytes: {}", bytes_written, String::from_utf8_lossy(data_to_write));
}
Err(e) => eprintln!("Write error: {}", e),
}
// Buffer to read data into.
let mut read_buffer = [0u8; 128];
// Read data from the buffer.
match shm.read(&mut read_buffer) {
Ok(bytes_read) => {
println!(
"Read {} bytes: {}",
bytes_read,
String::from_utf8_lossy(&read_buffer[..bytes_read])
);
}
Err(e) => eprintln!("Read error: {}", e),
}
}Contributions are welcome! Please feel free to submit a pull request.
A special thanks to codelif for his implementation of a Shared Memory Stream in Golang, which served as a great inspiration for this project: shmstream