|
| 1 | +# Code style |
| 2 | + |
| 3 | +smoltcp does not follow the rustfmt code style because whitequark (the original |
| 4 | +author of smoltcp) finds automated formatters annoying and impairing readability |
| 5 | +just as much as improving it in different cases. |
| 6 | + |
| 7 | +In general, format the things like the existing code and it'll be alright. |
| 8 | +Here are a few things to watch out for, though: |
| 9 | + |
| 10 | +## Ordering use statements |
| 11 | + |
| 12 | +Use statements should be separated into two sections, uses from other crates and uses |
| 13 | +from the current crate. The latter would ideally be sorted from most general |
| 14 | +to most specific, but it's not very important. |
| 15 | + |
| 16 | +```rust |
| 17 | +use core::cell::RefCell; |
| 18 | + |
| 19 | +use {Error, Result}; |
| 20 | +use phy::{self, DeviceCapabilities, Device}; |
| 21 | +``` |
| 22 | + |
| 23 | +## Wrapping function calls |
| 24 | + |
| 25 | +Avoid rightwards drift. This is fine: |
| 26 | + |
| 27 | +```rust |
| 28 | +assert_eq!(iface.inner.process_ethernet(&mut socket_set, 0, frame.into_inner()), |
| 29 | + Ok(Packet::None)); |
| 30 | +``` |
| 31 | + |
| 32 | +This is also fine: |
| 33 | + |
| 34 | +```rust |
| 35 | +assert_eq!(iface.inner.lookup_hardware_addr(MockTxToken, 0, |
| 36 | + &IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])), |
| 37 | + &IpAddress::Ipv4(remote_ip_addr)), |
| 38 | + Ok((remote_hw_addr, MockTxToken))); |
| 39 | +``` |
| 40 | + |
| 41 | +This is not: |
| 42 | + |
| 43 | +```rust |
| 44 | +assert_eq!(iface.inner.lookup_hardware_addr(MockTxToken, 0, |
| 45 | + &IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])), |
| 46 | + &IpAddress::Ipv4(remote_ip_addr)), |
| 47 | + Ok((remote_hw_addr, MockTxToken))); |
| 48 | +``` |
| 49 | + |
| 50 | +## Wrapping function prototypes |
| 51 | + |
| 52 | +A function declaration might be wrapped... |
| 53 | + |
| 54 | + * right after `,`, |
| 55 | + * right after `>`, |
| 56 | + * right after `)`, |
| 57 | + * right after `->`, |
| 58 | + * right before and after `where`. |
| 59 | + |
| 60 | +Here's an artificial example, wrapped at 50 columns: |
| 61 | + |
| 62 | +```rust |
| 63 | +fn dispatch_ethernet<Tx, F> |
| 64 | + (&mut self, tx_token: Tx, |
| 65 | + timestamp: u64, f: F) -> |
| 66 | + Result<()> |
| 67 | + where Tx: TxToken, |
| 68 | + F: FnOnce(EthernetFrame<&mut [u8]>) |
| 69 | +{ |
| 70 | + // ... |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Visually aligning tokens |
| 75 | + |
| 76 | +This is fine: |
| 77 | + |
| 78 | +```rust |
| 79 | +struct State { |
| 80 | + rng_seed: u32, |
| 81 | + refilled_at: u64, |
| 82 | + tx_bucket: u64, |
| 83 | + rx_bucket: u64, |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +This is also fine: |
| 88 | + |
| 89 | +```rust |
| 90 | +struct State { |
| 91 | + rng_seed: u32, |
| 92 | + refilled_at: u64, |
| 93 | + tx_bucket: u64, |
| 94 | + rx_bucket: u64, |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +It's OK to change between those if you touch that code anyway, |
| 99 | +but avoid reformatting just for the sake of it. |
0 commit comments