|
| 1 | +# Copilot Instructions for aspeed-ddk |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +aspeed-ddk is a Rust-based driver development kit for ASPEED SoCs, focusing on no_std environments and efficient resource usage. |
| 5 | + |
| 6 | +## Pull Request Review Checklist |
| 7 | + |
| 8 | +- [ ] Code is completely panic-free (no unwrap/expect/panic/indexing) |
| 9 | +- [ ] All fallible operations return Result or Option |
| 10 | +- [ ] Integer operations use checked/saturating/wrapping methods where needed |
| 11 | +- [ ] Array/slice access uses get() or pattern matching, not direct indexing |
| 12 | +- [ ] Error cases are well documented and handled appropriately |
| 13 | +- [ ] Tests verify error handling paths, not just happy paths |
| 14 | + |
| 15 | +## Quick Reference: Forbidden Patterns |
| 16 | + |
| 17 | +| Forbidden Pattern | Required Alternative | |
| 18 | +|-------------------|----------------------| |
| 19 | +| `value.unwrap()` | `match value { Some(v) => v, None => return Err(...) }` | |
| 20 | +| `result.expect("msg")` | `match result { Ok(v) => v, Err(e) => return Err(e.into()) }` | |
| 21 | +| `collection[index]` | `collection.get(index).ok_or(Error::OutOfBounds)?` | |
| 22 | + |
| 23 | + |
| 24 | +## Code Style |
| 25 | + |
| 26 | +### no_std and Memory Allocation Guidelines |
| 27 | + |
| 28 | +- This project is strictly **no_std** and **no_alloc** in production code |
| 29 | +- All production paths must be allocation-free and compatible with bare-metal targets |
| 30 | + |
| 31 | +#### Production Code Requirements |
| 32 | + |
| 33 | +- **NEVER** use crates or features that require heap allocation in production code |
| 34 | +- **DO NOT** use the `heapless` crate in production paths despite its name suggesting compatibility |
| 35 | +- **DO NOT** use any crate that depends on the `alloc` crate without feature gating |
| 36 | +- **ALWAYS** use fixed-size arrays, slices, or static memory allocation |
| 37 | +- **ALWAYS** design APIs to accept and return memory provided by the caller |
| 38 | + |
| 39 | +#### Memory Management in Production Code |
| 40 | + |
| 41 | +- Buffers must be pre-allocated by the caller and passed as slices |
| 42 | +- Collection types must have fixed, compile-time sizes |
| 43 | +- All data structures must have predictable, static memory footprints |
| 44 | +- No dynamic memory growth patterns are allowed |
| 45 | + |
| 46 | +#### Test Code Exceptions |
| 47 | + |
| 48 | +- Test code (annotated with `#[cfg(test)]`) may use allocation if needed |
| 49 | +- The `heapless` crate and other no_std compatible collections are permitted in tests |
| 50 | +- Standard library features may be used in tests when the `std` feature is enabled |
| 51 | +- Test helpers can use more ergonomic APIs that wouldn't be appropriate for production |
| 52 | + |
| 53 | +#### Example: Production vs. Test Code |
| 54 | + |
| 55 | +```rust |
| 56 | +// Production code - strict no_alloc approach |
| 57 | +pub fn process_data(data: &[u8], output: &mut [u8]) -> Result<usize, Error> { |
| 58 | + if output.len() < data.len() * 2 { |
| 59 | + return Err(Error::BufferTooSmall); |
| 60 | + } |
| 61 | + |
| 62 | + // Process data into output buffer |
| 63 | + // ... |
| 64 | + |
| 65 | + Ok(processed_bytes) |
| 66 | +} |
| 67 | + |
| 68 | +// Test code - can use more ergonomic approaches |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use super::*; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_process_data() { |
| 75 | + // It's fine to use Vec in tests |
| 76 | + let input = vec![1, 2, 3, 4]; |
| 77 | + let mut output = vec![0; 16]; |
| 78 | + |
| 79 | + let result = process_data(&input, &mut output); |
| 80 | + assert!(result.is_ok()); |
| 81 | + // ... |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_with_heapless() { |
| 86 | + // Heapless is also fine in tests |
| 87 | + use heapless::Vec; |
| 88 | + let mut input: Vec<u8, 8> = Vec::new(); |
| 89 | + input.extend_from_slice(&[1, 2, 3]).unwrap(); |
| 90 | + |
| 91 | + let mut output = [0u8; 16]; |
| 92 | + let result = process_data(&input, &mut output); |
| 93 | + assert!(result.is_ok()); |
| 94 | + // ... |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +### Unsafe Code |
| 100 | + |
| 101 | +- Minimize unsafe code; isolate in well-documented functions |
| 102 | +- Document all safety preconditions in unsafe functions |
| 103 | +- Add safety comments explaining why unsafe is necessary |
| 104 | +- Unit test unsafe code thoroughly |
| 105 | + |
| 106 | +## Common Patterns |
| 107 | + |
| 108 | +### Static vs. Dynamic Dispatch |
| 109 | + |
| 110 | +This project strongly prefers static dispatch over dynamic dispatch to optimize for binary size, performance, and no_std compatibility. |
| 111 | + |
| 112 | +#### Static Dispatch Requirements |
| 113 | + |
| 114 | +- Use generic parameters instead of trait objects (`dyn Trait`) whenever possible |
| 115 | +- Leverage impl Trait in return positions rather than Box<dyn Trait> |
| 116 | +- Monomorphize code at compile time rather than using virtual dispatch at runtime |
| 117 | +- Avoid heap allocations associated with typical dyn Trait usage |
| 118 | + |
| 119 | + |
| 120 | +## Workflows |
| 121 | + |
| 122 | +### Pre-commit |
| 123 | +cargo xtask precommit |
0 commit comments