Searpc-rs: Rust implementation of libsearpc RPC protocol
A minimal, type-safe Rust implementation of the Searpc RPC protocol, compatible with libsearpc C and pysearpc Python implementations.
Design Philosophy ("Good Taste" Code)
This implementation follows Linus Torvalds' principles of good code:
-
Data structures first: Use enums instead of strcmp()
- C:
if (strcmp(type, "int") == 0) ... - Rust:
enum Arg { Int(i32), String(String), ... }
- C:
-
Eliminate special cases: No edge-case handling
- Single code path for all operations
- Use
?operator for uniform error handling
-
Memory safety by design: Zero unsafe code
- Automatic RAII vs manual g_free()
- Compiler-verified lifetime management
Protocol Format
Request (JSON array)
Response (JSON object)
Transport Protocols
libsearpc has two different packet protocols:
1. TCP Demo Protocol (16-bit header)
- Used by: C demo server, simple testing
- Header: 16-bit big-endian length (max 64KB)
- Format:
<u16 len><json> - Implementation: [
TcpTransport]
2. Unix Socket Protocol (32-bit header + wrapper)
- Used by: Seafile production, pysearpc
- Header: 32-bit native-endian length
- Format:
<u32 len>{"service": "name", "request": [...]} - Implementation: [
UnixSocketTransport]
Critical: Do not mix these protocols! Seafile requires Unix Socket protocol.
API Comparison
C (libsearpc)
// 10+ lines of parameter marshalling with strcmp()
if
;
else if
;
// ... more strcmp() branches
Rust (searpc-rs)
# use ;
# let transport = connect.unwrap;
# let mut client = new;
// Type-safe, zero branches
let result = client.call_int;
Example Usage
Synchronous API
use ;
#
Async API (with tokio)
use ;
#
# async
Feature Completeness vs C Implementation
✅ Implemented:
- All 6 RPC types: int, int64, string, object, objlist, json
- Both transport protocols (TCP 16-bit + Unix Socket 32-bit)
- NULL parameter support (via
Arg::Null) - Error handling (matches C's TRANSPORT_ERROR_CODE 500)
- Type-safe API with compile-time checking
✅ Async Support (optional, enabled by default):
- Async API with tokio runtime
- [
AsyncSearpcClient] for async operations - [
AsyncTcpTransport] for async TCP - Disable with
default-features = false
⏳ Future (not needed for basic usage):
- Connection pooling
- Procedural macros for convenience
- Server implementation
Code Metrics
- 745 lines vs C's ~2000 lines (-63%)
- Zero unsafe blocks
- Zero compilation warnings
- 14 unit tests (all passing)
- 100% C compatibility (verified with demo server)