Expand description
§r-resources
Android-style resource management for Rust with compile-time type safety.
This library provides a build-time resource management system inspired by Android’s R class.
Resources are defined in an XML file and compiled into type-safe Rust constants at build time,
resulting in zero runtime overhead.
§Quick Start
- Create a
res/values.xmlfile in your project root:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My Application</string>
<int name="max_retries">3</int>
<float name="version">1.0</float>
</resources>- Include resources in your code:
ⓘ
use r_resources::include_resources;
include_resources!();
use r_resources::*;
// Option 1: Type-organized access
let _ = string::APP_NAME;
let _ = int::MAX_RETRIES;
let _ = float::VERSION;
// Option 2: Flat access via r module
let _ = r::APP_NAME;
let _ = r::MAX_RETRIES;
let _ = r::VERSION;§Supported Resource Types
- Strings:
<string name="key">value</string>→string::KEYorr::KEY - Integers:
<int name="key">42</int>→int::KEYorr::KEY - Floats:
<float name="key">3.14</float>→float::KEYorr::KEY - String Arrays:
<string-array name="key">...</string-array>→string_array::KEYorr::KEY - Integer Arrays:
<int-array name="key">...</int-array>→int_array::KEYorr::KEY - Float Arrays:
<float-array name="key">...</float-array>→float_array::KEYorr::KEY
Both access methods are available:
- Type-organized:
string::APP_NAME(clearer, avoids naming conflicts) - Flat access:
r::APP_NAME(shorter, more convenient)
§Features
- Build-time compilation: All resources are compiled into your binary
- Type-safe: Each resource type has its own module
- Zero runtime cost: Direct constant access, no parsing or lookups
- Thread-safe: All resources are
constand can be safely accessed from any thread - Async-safe: Works seamlessly in async contexts (tokio, async-std, etc.)
- Familiar syntax: Inspired by Android’s resource system
§Thread Safety
All generated resources are const values, making them inherently thread-safe:
ⓘ
use std::thread;
use r_resources::*;
let handles: Vec<_> = (0..10)
.map(|_| {
thread::spawn(|| {
// Safe to access from multiple threads
println!("{}", string::APP_NAME);
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}Macros§
- include_
resources - Includes the generated resources from the build script.
Structs§
- Color
- Typed color parsed from hex (e.g.,
#RRGGBBor#AARRGGBB). - LatLng
- Geographic coordinates.
- Position
- 2D position.
- UrlParts
- Typed URL parts split at build-time.
Functions§
- build
- Runs the code generation. Intended to be called from a consumer’s build.rs.