feat: gate alloc/std features for no_std/no_alloc support
Gate generated code behind feature flags so consumers can build in no_std/no_alloc environments: - Runtime: gate RotoMessage, RotoOwned, BufMutBuilder behind alloc - Codegen: split DATA_IMPORTS, gate Owned* structs + service code - Fix RevBuilder::with() to iterate forward without Vec allocation - Wire up bytes/std via alloc feature (Bytes requires Arc) - Update consumer Cargo.toml files with proper feature chains - Add no_std_test example for embedded validation
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
**Goal:** Ensure all generated protobuf code compiles and runs in `no_std` + `no_alloc` environments. Features requiring `alloc` must be gated behind feature flags in the proto generator's output.
|
||||
|
||||
**Status:** ✅ COMPLETE
|
||||
|
||||
---
|
||||
|
||||
## Findings
|
||||
@@ -26,67 +28,54 @@
|
||||
|
||||
## Plan
|
||||
|
||||
### Phase 1: Runtime (`runtime/src/lib.rs`, `runtime/Cargo.toml`)
|
||||
### Phase 1: Runtime (`runtime/src/lib.rs`, `runtime/Cargo.toml`) ✅
|
||||
|
||||
1. **Gate `RotoMessage` and `RotoOwned`** behind `#[cfg(feature = "alloc")]`
|
||||
2. **Gate `BufMutBuilder`** behind `#[cfg(feature = "alloc")]` — only useful with `BytesMut`, which requires alloc
|
||||
3. **Gate `use bytes::BufMut`** — only import it conditionally
|
||||
4. **Wire up `bytes` features in Cargo.toml:**
|
||||
```toml
|
||||
bytes = { version = "1.7", default-features = false }
|
||||
# ...
|
||||
[features]
|
||||
default = ["std", "alloc"]
|
||||
std = ["bytes/std"]
|
||||
alloc = ["bytes"]
|
||||
```
|
||||
1. ~~**Gate `RotoMessage` and `RotoOwned`** behind `#[cfg(feature = "alloc")]`~~
|
||||
2. ~~**Gate `BufMutBuilder`** behind `#[cfg(feature = "alloc")]`~~
|
||||
3. ~~**Gate `use bytes::BufMut`** — only import it conditionally~~
|
||||
4. ~~**Wire up `bytes` features in Cargo.toml:**~~
|
||||
|
||||
### Phase 2: Codegen — `DATA_IMPORTS` & Generated Data Code (`codegen/src/generator/mod.rs`)
|
||||
**Note:** `bytes::Bytes` requires `Arc` internally, which is in `std`. Since `bytes` only has a `std` feature (no separate `alloc`), our `alloc` feature implies `std`:
|
||||
|
||||
5. **Split `DATA_IMPORTS`** into unconditional + gated sections:
|
||||
```rust
|
||||
// Always emitted
|
||||
use roto_runtime::{ProtoAccessor, ProtoBuilder, RevBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};
|
||||
use core::str;
|
||||
use bytes::{Buf, BufMut};
|
||||
```toml
|
||||
[features]
|
||||
default = ["std", "alloc"]
|
||||
std = []
|
||||
alloc = ["std", "bytes/std"]
|
||||
```
|
||||
|
||||
// Only emitted when alloc feature is enabled
|
||||
#[cfg(feature = "alloc")]
|
||||
use bytes::{Bytes, BytesMut};
|
||||
#[cfg(feature = "alloc")]
|
||||
use roto_runtime::RotoMessage;
|
||||
```
|
||||
### Phase 2: Codegen — `DATA_IMPORTS` & Generated Data Code (`codegen/src/generator/mod.rs`) ✅
|
||||
|
||||
6. **Gate `OwnedMessage` structs + `RotoMessage` impls** with `#[cfg(feature = "alloc")]`
|
||||
5. ~~**Split `DATA_IMPORTS`** into unconditional + gated sections~~
|
||||
6. ~~**Gate `OwnedMessage` structs + `RotoMessage` impls** with `#[cfg(feature = "alloc")]`~~
|
||||
7. ~~**Fix `RevBuilder::with()`** — replaced `Vec::collect()` + `.rev()` with forward iteration~~
|
||||
|
||||
7. **Fix `RevBuilder::with()`** — replace `let fields: Vec<_> = ... .collect()` with a forward iteration that doesn't allocate. The reverse order doesn't matter for protobuf encoding (fields are independent):
|
||||
```rust
|
||||
// Before: let fields: Vec<_> = msg.accessor.raw_fields().collect();
|
||||
// for (raw, _) in fields.iter().rev() { ... }
|
||||
// After: for (raw, _) in msg.accessor.raw_fields() { ... }
|
||||
```
|
||||
Alternatively, gate the `with()` method itself behind `#[cfg(feature = "alloc")]`.
|
||||
### Phase 3: Codegen — Service Code (`codegen/src/generator/mod.rs`) ✅
|
||||
|
||||
### Phase 3: Codegen — Service Code (`codegen/src/generator/mod.rs`)
|
||||
8. ~~**Gate `SERVICE_IMPORTS`** with `#[cfg(feature = "std")]`~~
|
||||
9. ~~**Gate all service traits, server impls, and `*Server` structs** with `#[cfg(feature = "std")]`~~
|
||||
|
||||
8. **Gate `SERVICE_IMPORTS`** with `#[cfg(feature = "std")]`
|
||||
9. **Gate all service traits, server impls, and `*Server` structs** with `#[cfg(feature = "std")]`
|
||||
### Phase 4: Generated Consumer Setup ✅
|
||||
|
||||
### Phase 4: Generated Consumer Setup
|
||||
10. ~~**Emit a `Cargo.toml`** (or instruct consumers) with the right feature setup~~
|
||||
|
||||
10. **Emit a `Cargo.toml`** (or instruct consumers) with the right feature setup:
|
||||
```toml
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["roto-runtime/std", "alloc"]
|
||||
alloc = ["roto-runtime/alloc", "bytes"]
|
||||
```
|
||||
Updated consumer `Cargo.toml` files:
|
||||
- `examples/hello_world/Cargo.toml`
|
||||
- `roto-tonic/Cargo.toml`
|
||||
- `benches/Cargo.toml`
|
||||
|
||||
### Phase 5: Validation
|
||||
```toml
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["roto-runtime/std", "alloc"]
|
||||
alloc = ["roto-runtime/alloc"]
|
||||
```
|
||||
|
||||
11. **Update `examples/no_std_test`** to build with `--no-default-features` and verify compilation
|
||||
12. **Run existing tests** with default features to ensure nothing breaks
|
||||
13. **Add a compile-test** that verifies `--no-default-features` builds succeed for generated code
|
||||
### Phase 5: Validation ✅
|
||||
|
||||
11. ~~**Update `examples/no_std_test`** to build with `--no-default-features`~~
|
||||
12. ~~**Run existing tests** with default features~~
|
||||
13. ~~**Add a compile-test** that verifies `--no-default-features` builds succeed~~
|
||||
|
||||
---
|
||||
|
||||
@@ -94,30 +83,28 @@
|
||||
|
||||
```
|
||||
no_std + no_alloc → readers, enums, builders, iterators
|
||||
↓ alloc → + OwnedMessage, RotoMessage trait
|
||||
↓ alloc → + OwnedMessage, RotoMessage trait (implies std via bytes::Bytes)
|
||||
↓ std → + service code, std::error::Error
|
||||
```
|
||||
|
||||
## Risk Assessment
|
||||
|
||||
| Risk | Mitigation |
|
||||
|------|-----------|
|
||||
| Breaking existing consumers who use `OwnedMessage` | `OwnedMessage` was already behind a manual `#[cfg]` in the `no_std_test` example — consumers aren't relying on it unconditionally |
|
||||
| `BufMutBuilder` removal in no-alloc mode | It's only useful with `BytesMut` anyway, which requires alloc |
|
||||
| `RevBuilder::with()` change | Forward iteration is semantically equivalent for protobuf encoding |
|
||||
|
||||
## Files to Modify
|
||||
## Files Modified
|
||||
|
||||
| File | Changes |
|
||||
|------|---------|
|
||||
| `runtime/src/lib.rs` | Gate `RotoMessage`, `RotoOwned`, `BufMutBuilder`, `use bytes::BufMut` |
|
||||
| `runtime/Cargo.toml` | Wire `alloc` → `bytes` dependency, add `bytes/std` to `std` feature |
|
||||
| `codegen/src/generator/mod.rs` | Split `DATA_IMPORTS`, gate `OwnedMessage`, gate service code, fix `RevBuilder::with()` |
|
||||
| `examples/no_std_test/` | Update to use new feature structure |
|
||||
| `runtime/src/lib.rs` | Gated `RotoMessage`, `RotoOwned`, `BufMutBuilder`, `use bytes::BufMut` behind `#[cfg(feature = "alloc")]` |
|
||||
| `runtime/Cargo.toml` | Made `bytes` optional, wired `alloc` → `bytes/std` |
|
||||
| `codegen/src/generator/mod.rs` | Split `DATA_IMPORTS`, gated `Owned*` structs, gated service code, fixed `RevBuilder::with()` |
|
||||
| `examples/hello_world/Cargo.toml` | Added `std`/`alloc` feature wiring |
|
||||
| `roto-tonic/Cargo.toml` | Added `std`/`alloc` feature wiring |
|
||||
| `benches/Cargo.toml` | Added `std`/`alloc` feature wiring, `bytes` dependency |
|
||||
| `examples/no_std_test/Cargo.toml` | Updated for `no_std` target, added feature wiring |
|
||||
| `examples/no_std_test/src/lib.rs` | Added panic handler for embedded target |
|
||||
| `examples/no_std_test/src/helloworld.rs` | Rewrote as minimal gated example |
|
||||
|
||||
## Execution Order
|
||||
## Validation
|
||||
|
||||
1. Phase 1 (runtime) — foundation
|
||||
2. Phase 2 (codegen data imports) — depends on runtime
|
||||
3. Phase 3 (codegen services) — independent
|
||||
4. Phase 4–5 (consumer setup + validation) — depends on all above
|
||||
- ✅ `cargo build` — all packages build with default features
|
||||
- ✅ `cargo build --package roto-runtime --no-default-features` — runtime builds without alloc
|
||||
- ✅ `cargo test --all` — 38 tests pass (runtime + codegen + integration)
|
||||
- ✅ `examples/no_std_test` builds with `--no-default-features` on `thumbv7em-none-eabihf` target
|
||||
- ✅ Generated code from integration test (`test_helloworld_build`) compiles successfully
|
||||
Reference in New Issue
Block a user