Skip to content

Commit 32ee91d

Browse files
committedNov 3, 2017
Add CODE_STYLE.md.
1 parent 198fe23 commit 32ee91d

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed
 

‎CODE_STYLE.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.

‎src/phy/fault_injector.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ pub struct RxToken<'a, Rx: phy::RxToken> {
238238
}
239239

240240
impl<'a, Rx: phy::RxToken> phy::RxToken for RxToken<'a, Rx> {
241-
fn consume<R, F: FnOnce(&[u8]) -> Result<R>>(self, timestamp: u64, f: F) -> Result<R> {
241+
fn consume<R, F>(self, timestamp: u64, f: F) -> Result<R>
242+
where F: FnOnce(&[u8]) -> Result<R>
243+
{
242244
if self.state.borrow_mut().maybe(self.config.drop_pct) {
243245
net_trace!("rx: randomly dropping a packet");
244246
return Err(Error::Exhausted)
@@ -275,8 +277,8 @@ pub struct TxToken<'a, Tx: phy::TxToken> {
275277
}
276278

277279
impl<'a, Tx: phy::TxToken> phy::TxToken for TxToken<'a, Tx> {
278-
fn consume<R, F: FnOnce(&mut [u8]) -> Result<R>>(mut self, timestamp: u64, len: usize, f: F)
279-
-> Result<R>
280+
fn consume<R, F>(mut self, timestamp: u64, len: usize, f: F) -> Result<R>
281+
where F: FnOnce(&mut [u8]) -> Result<R>
280282
{
281283
let drop = if self.state.borrow_mut().maybe(self.config.drop_pct) {
282284
net_trace!("tx: randomly dropping a packet");

0 commit comments

Comments
 (0)
Please sign in to comment.