Add CODE_STYLE.md.
parent
6a8e21cec0
commit
9e783cb570
|
@ -0,0 +1,99 @@
|
|||
# Code style
|
||||
|
||||
smoltcp does not follow the rustfmt code style because whitequark (the original
|
||||
author of smoltcp) finds automated formatters annoying and impairing readability
|
||||
just as much as improving it in different cases.
|
||||
|
||||
In general, format the things like the existing code and it'll be alright.
|
||||
Here are a few things to watch out for, though:
|
||||
|
||||
## Ordering use statements
|
||||
|
||||
Use statements should be separated into two sections, uses from other crates and uses
|
||||
from the current crate. The latter would ideally be sorted from most general
|
||||
to most specific, but it's not very important.
|
||||
|
||||
```rust
|
||||
use core::cell::RefCell;
|
||||
|
||||
use {Error, Result};
|
||||
use phy::{self, DeviceCapabilities, Device};
|
||||
```
|
||||
|
||||
## Wrapping function calls
|
||||
|
||||
Avoid rightwards drift. This is fine:
|
||||
|
||||
```rust
|
||||
assert_eq!(iface.inner.process_ethernet(&mut socket_set, 0, frame.into_inner()),
|
||||
Ok(Packet::None));
|
||||
```
|
||||
|
||||
This is also fine:
|
||||
|
||||
```rust
|
||||
assert_eq!(iface.inner.lookup_hardware_addr(MockTxToken, 0,
|
||||
&IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])),
|
||||
&IpAddress::Ipv4(remote_ip_addr)),
|
||||
Ok((remote_hw_addr, MockTxToken)));
|
||||
```
|
||||
|
||||
This is not:
|
||||
|
||||
```rust
|
||||
assert_eq!(iface.inner.lookup_hardware_addr(MockTxToken, 0,
|
||||
&IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])),
|
||||
&IpAddress::Ipv4(remote_ip_addr)),
|
||||
Ok((remote_hw_addr, MockTxToken)));
|
||||
```
|
||||
|
||||
## Wrapping function prototypes
|
||||
|
||||
A function declaration might be wrapped...
|
||||
|
||||
* right after `,`,
|
||||
* right after `>`,
|
||||
* right after `)`,
|
||||
* right after `->`,
|
||||
* right before and after `where`.
|
||||
|
||||
Here's an artificial example, wrapped at 50 columns:
|
||||
|
||||
```rust
|
||||
fn dispatch_ethernet<Tx, F>
|
||||
(&mut self, tx_token: Tx,
|
||||
timestamp: u64, f: F) ->
|
||||
Result<()>
|
||||
where Tx: TxToken,
|
||||
F: FnOnce(EthernetFrame<&mut [u8]>)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## Visually aligning tokens
|
||||
|
||||
This is fine:
|
||||
|
||||
```rust
|
||||
struct State {
|
||||
rng_seed: u32,
|
||||
refilled_at: u64,
|
||||
tx_bucket: u64,
|
||||
rx_bucket: u64,
|
||||
}
|
||||
```
|
||||
|
||||
This is also fine:
|
||||
|
||||
```rust
|
||||
struct State {
|
||||
rng_seed: u32,
|
||||
refilled_at: u64,
|
||||
tx_bucket: u64,
|
||||
rx_bucket: u64,
|
||||
}
|
||||
```
|
||||
|
||||
It's OK to change between those if you touch that code anyway,
|
||||
but avoid reformatting just for the sake of it.
|
|
@ -238,7 +238,9 @@ pub struct RxToken<'a, Rx: phy::RxToken> {
|
|||
}
|
||||
|
||||
impl<'a, Rx: phy::RxToken> phy::RxToken for RxToken<'a, Rx> {
|
||||
fn consume<R, F: FnOnce(&[u8]) -> Result<R>>(self, timestamp: u64, f: F) -> Result<R> {
|
||||
fn consume<R, F>(self, timestamp: u64, f: F) -> Result<R>
|
||||
where F: FnOnce(&[u8]) -> Result<R>
|
||||
{
|
||||
if self.state.borrow_mut().maybe(self.config.drop_pct) {
|
||||
net_trace!("rx: randomly dropping a packet");
|
||||
return Err(Error::Exhausted)
|
||||
|
@ -275,8 +277,8 @@ pub struct TxToken<'a, Tx: phy::TxToken> {
|
|||
}
|
||||
|
||||
impl<'a, Tx: phy::TxToken> phy::TxToken for TxToken<'a, Tx> {
|
||||
fn consume<R, F: FnOnce(&mut [u8]) -> Result<R>>(mut self, timestamp: u64, len: usize, f: F)
|
||||
-> Result<R>
|
||||
fn consume<R, F>(mut self, timestamp: u64, len: usize, f: F) -> Result<R>
|
||||
where F: FnOnce(&mut [u8]) -> Result<R>
|
||||
{
|
||||
let drop = if self.state.borrow_mut().maybe(self.config.drop_pct) {
|
||||
net_trace!("tx: randomly dropping a packet");
|
||||
|
|
Loading…
Reference in New Issue