Skip to content

Commit af01162

Browse files
committed
Fix mdbook build, add travis-ci, and publish book
Closes #1 . Closes #27 .
1 parent c4c1859 commit af01162

15 files changed

+97
-68
lines changed

Diff for: .travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: rust
2+
sudo: false
3+
rust: nightly
4+
5+
install: cargo install mdbook
6+
script: cd reference && mdbook build && mdbook test
7+
deploy:
8+
provider: pages
9+
skip-cleanup: true
10+
github-token: $GITHUB_TOKEN
11+
local-dir: reference/book
12+
keep-history: false
13+
on:
14+
branch: master

Diff for: README.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# unsafe-code-guidelines
22

3+
[![Travis-CI Status]][travis]
4+
35
Home for the Unsafe Code Guidelines (UCG) effort. The goal of the Unsafe
46
Code Guidelines effort is to collaboratively produce a "reference
57
guide" for writing unsafe code that what kinds of things unsafe code
@@ -49,6 +51,18 @@ Guidelines Reference" here.][rr]
4951

5052
[rr]: https://github.com/rust-rfcs/unsafe-code-guidelines/blob/master/reference/src/SUMMARY.md
5153

54+
### Build instructions
55+
56+
Make sure that `mdbook` is installed:
57+
58+
> cargo install mdbook
59+
60+
and execute `mdbook build` or `mdbook serve` in the `reference/` directory.
61+
62+
### Link to the current version of the book
63+
64+
[Here](https://rust-rfcs.github.io/unsafe-code-guidelines/book).
65+
5266
## Chat platform, discussion cadence
5367

5468
Most of the discussion takes place here in GitHub issues. Many of us
@@ -77,3 +91,6 @@ effort to explain how to write Rust code, rather than a reference.
7791
[The nikomatsakis/rust-memory-model
7892
repository](https://github.com/nikomatsakis/rust-memory-model) was a
7993
previous effort and contains a lot of good links and info.
94+
95+
[travis]: https://travis-ci.org/rust-rfcs/unsafe-code-guidelines
96+
[Travis-CI Status]: https://travis-ci.org/rust-rfcs/unsafe-code-guidelines.svg?branch=master

Diff for: reference/src/SUMMARY.md

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
# Summary
22

3-
- [Introduction](./introduction.md)
4-
- [Areas of active discussion](./active_discussion.md)
5-
- [Aliasing and memory model](./active_discussion/aliasing.md)
6-
- [Cryptographic concerns](./active_discussion/crypto_concerns.md)
7-
- [Keeping secrets](./active_discussion/crypto_concerns/keeping_secrets.md)
8-
- [Constant time code](./active_discussion/crypto_concerns/constant_time_code.md)
9-
- [Zeroing](./active_discussion/crypto_concerns/zeroing.md)
10-
- [Data structure layout](./active_discussion/layout.md)
11-
- [Stable addresses](./active_discussion/stable_addresses.md)
12-
- [Storage liveness](./active_discussion/storage_liveness.md)
13-
- [Uninhabited types like `!` and exhaustiveness](./active_discussion/uninhabited_types.md)
14-
- [Unions](./active_discussion/unions.md)
15-
- [Uninitialized memory](./active_discussion/uninitialized_memory.md)
3+
[Glossary](./glossary.md)
4+
165
- [Data layout](./layout.md)
176
- [Structs and tuples](./layout/structs-and-tuples.md)
187
- [Integers and Floating Points](./layout/integers-floatingpoint.md)
@@ -21,5 +10,4 @@
2110
- [Arrays and Slices](./layout/arrays-and-slices.md)
2211
- [Packed SIMD vectors](./layout/packed-simd-vectors.md)
2312
- [Optimizations](./optimizations.md)
24-
- [Optimizing immutable memory](./optimizations/immutable_memory.md)
25-
- [Glossary](./glossary.md)
13+
- [Return value optimization](./optimizations/return_value_optimization.md)

Diff for: reference/src/chapter_1.md

-1
This file was deleted.

Diff for: reference/src/glossary.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Moreover, arguments passed to a function must be valid at the type given in the
2525
OPEN QUESTION: Are there more cases where data must be valid?
2626

2727
In terms of code, some data computed by `TERM` is valid at type `T` if and only if the following program does not have UB:
28-
```rust
28+
```rust,ignore
2929
fn main() { unsafe {
3030
let t: T = std::mem::transmute(TERM);
3131
} }
@@ -37,7 +37,7 @@ The safety invariant can be temporarily violated by unsafe code, but must always
3737
It is not relevant when arguing whether some *program* has UB, but it is relevant when arguing whether some code safely encapsulates its unsafety -- IOW, it is relevant when arguing whether some *library* can be used by safe code to *cause* UB.
3838

3939
In terms of code, some data computed by `TERM` (possibly constructed from some `arguments` that can be *assumed* to satisfy the safety invariant) is valid at type `T` if and only if the following library function can be safely exposed to arbitrary (safe) code as part of the public library interface:
40-
```rust
40+
```rust,ignore
4141
pub fn make_something(arguments: U) -> T { unsafe {
4242
std::mem::transmute(TERM)
4343
} }

Diff for: reference/src/introduction.md

-3
This file was deleted.

Diff for: reference/src/layout.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Data layout

Diff for: reference/src/layout/enums.md

+45-7
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,22 @@ enum TwoCases {
181181
This will be laid out equivalently to the following more
182182
complex Rust types:
183183

184-
```
184+
```rust
185185
#[repr(C)]
186186
union TwoCasesRepr {
187187
A: TwoCasesVariantA,
188188
B: TwoCasesVariantB,
189189
}
190190

191+
# #[derive(Copy, Clone)]
191192
#[repr(u8)]
192193
enum TwoCasesTag { A, B }
193194

195+
# #[derive(Copy, Clone)]
194196
#[repr(C)]
195197
struct TwoCasesVariantA(TwoCasesTag, u8, u16);
196198

199+
# #[derive(Copy, Clone)]
197200
#[repr(C)]
198201
struct TwoCasesVariantB(TwoCasesTag, u16);
199202
```
@@ -222,7 +225,7 @@ occupy 6 bytes with `#[repr(C, u8)]`, as more padding is required.
222225

223226
**Example.** The following enum:
224227

225-
```rust
228+
```rust,ignore
226229
#[repr(C, Int)]
227230
enum MyEnum {
228231
A(u32),
@@ -234,7 +237,7 @@ enum MyEnum {
234237

235238
is equivalent to the following Rust definition:
236239

237-
```rust
240+
```rust,ignore
238241
#[repr(C)]
239242
struct MyEnumRepr {
240243
tag: MyEnumTag,
@@ -257,7 +260,6 @@ struct MyEnumPayloadB(f32, u64);
257260
258261
#[repr(C)]
259262
struct MyEnumPayloadC { x: u32, y: u8 }
260-
}
261263
```
262264

263265
This enum can also be represented in C++ as follows:
@@ -308,7 +310,7 @@ type `T`.
308310
309311
**Definition.** In some cases, the payload type may contain illegal
310312
values, which are called **niches**. For example, a value of type `&T`
311-
may never be NULL, and hence defines a niche consisting of the
313+
may never be `NULL`, and hence defines a niche consisting of the
312314
bitstring `0`. Similarly, the standard library types [`NonZeroU8`]
313315
and friends may never be zero, and hence also define the value of `0`
314316
as a niche. (Types that define niche values will say so as part of the
@@ -367,13 +369,25 @@ As presently implemented, the compiler will use the same layout for
367369
structs and for single variant enums (as long as they do not have a
368370
`#[repr]` annotation that overrides that choice). So, for example, the
369371
struct `SomeStruct` and the enum `SomeEnum` would have an equivalent
370-
layout ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3697ac684c3d021892694956df957653))::
372+
layout ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3697ac684c3d021892694956df957653)):
371373

372374
```rust
373375
struct SomeStruct;
374376
enum SomeEnum {
375377
SomeVariant,
376378
}
379+
380+
# fn main() {
381+
# use std::mem;
382+
let x = SomeStruct;
383+
let y = SomeEnum::SomeVariant;
384+
assert_eq!(
385+
mem::size_of_val(&x),
386+
mem::size_of_val(&y),
387+
"types have different sizes",
388+
);
389+
println!("types both have size {}", std::mem::size_of_val(&x));
390+
# }
377391
```
378392

379393
Similarly, the struct `SomeStruct` and the enum `SomeVariant` in this
@@ -385,6 +399,18 @@ struct SomeStruct { x: u32 }
385399
enum SomeEnum {
386400
SomeVariant { x: u32 },
387401
}
402+
403+
# fn main() {
404+
# use std::mem;
405+
let x = SomeStruct { x: 22 };
406+
let y = SomeEnum::SomeVariant { x: 22 };
407+
assert_eq!(
408+
mem::size_of_val(&x),
409+
mem::size_of_val(&y),
410+
"types have different sizes",
411+
);
412+
println!("types both have size {}", mem::size_of_val(&x));
413+
}
388414
```
389415

390416
In fact, the compiler will use this optimized layout even for enums
@@ -393,10 +419,22 @@ is uninhabited
393419
([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3cc1484c5b91097f3dc2015b7c207a0e)):
394420

395421
```rust
422+
# enum Void { }
396423
struct SomeStruct { x: u32 }
397424
enum SomeEnum {
398425
SomeVariant { x: u32 },
399426
UninhabitedVariant { y: Void },
400427
}
401-
```
402428

429+
# fn main() {
430+
# use std::mem;
431+
let x = SomeStruct { x: 22 };
432+
let y = SomeEnum::SomeVariant { x: 22 };
433+
assert_eq!(
434+
mem::size_of_val(&x),
435+
mem::size_of_val(&y),
436+
"types have different sizes",
437+
);
438+
println!("types both have size {}", mem::size_of_val(&x));
439+
# }
440+
```

Diff for: reference/src/layout/integers-floatingpoint.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Layout of Boolean, Floating Point, and Integral Types
22
This chapter represents the consensus from issue [#9]. It documents the memory layout and considerations for `bool`, `usize`, `isize`, floating point types, and integral types.
3+
34
[#9]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/9
45

56
## Overview
6-
These are all scalar types, representing a single value. These types have no layout variants (no #[repr(C)] or #[repr(Rust)]. Their size is fixed and well-defined across FFI boundaries and map to their corresponding integral types in the C ABI.
7+
These are all scalar types, representing a single value. These types have no layout variants (no `#[repr(C)]` or `#[repr(Rust)]`). Their size is fixed and well-defined across FFI boundaries and map to their corresponding integral types in the C ABI.
78
- `bool`: 1 byte
89
- any `bool` can be cast into an integer, taking on the values 1 (true) or 0 (false)
910
- `usize`, `isize`: pointer-sized unsigned/signed integer type
@@ -19,11 +20,11 @@ These are all scalar types, representing a single value. These types have no lay
1920
- not ABI compatible
2021
- represents [Unicode scalar value](http://www.unicode.org/glossary/#unicode_scalar_value)
2122

22-
##`usize`/`isize`
23+
## `usize`/`isize`
2324
Types `usize` and `isize` are committed to having the same size as a native pointer on the platform. The layout of `usize` determines the following:
2425
- how much a pointer of a certain type can be offseted,
2526
- the maximum size of Rust objects (because size_of/size_of_val return `usize`),
26-
- the maximum number of elements in an array ([T; N: usize]),
27+
- the maximum number of elements in an array (`[T; N: usize]`),
2728
- `usize`/`isize` in C FFI are compatible with C's `uintptr_t` / `intptr_t` (and have the same size and alignment).
2829

2930
The maximum size of any single value must fit within `usize` to [ensure that pointer diff is representable](https://github.com/rust-rfcs/unsafe-code-guidelines/pull/5#discussion_r212703192).

Diff for: reference/src/layout/packed-simd-vectors.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Packed SIMD vector types are `repr(simd)` homogeneous tuple-structs containing
2525
`N` elements of type `T` where `N` is a power-of-two and the size and alignment
2626
requirements of `T` are equal:
2727

28-
```rust
28+
```rust,ignore
2929
#[repr(simd)]
3030
struct Vector<T, N>(T_0, ..., T_(N - 1));
3131
```
@@ -36,7 +36,7 @@ The size of `Vector` is `N * size_of::<T>()` and its alignment is an
3636
_implementation-defined_ function of `T` and `N` greater than or equal to
3737
`align_of::<T>()`. That is:
3838

39-
```rust
39+
```rust,ignore
4040
assert_eq!(size_of::<Vector<T, N>>(), size_of::<T>() * N);
4141
assert!(align_of::<Vector<T, N>>() >= align_of::<T>());
4242
```
@@ -47,7 +47,7 @@ same `N` have the same size and alignment.
4747
Vector elements are laid out in source field order, enabling random access to
4848
vector elements by reinterpreting the vector as an array:
4949

50-
```rust
50+
```rust,ignore
5151
union U {
5252
vec: Vector<T, N>,
5353
arr: [T; N]
@@ -70,7 +70,7 @@ unsafe {
7070
* **Blocked**: Should the layout of packed SIMD vectors be the same as that of
7171
homogeneous tuples ? Such that:
7272

73-
```rust
73+
```rust,ignore
7474
union U {
7575
vec: Vector<T, N>,
7676
tup: (T_0, ..., T_(N-1)),

Diff for: reference/src/layout/structs-and-tuples.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ not to change until an RFC ratifies them.
1212
In general, an anonymous tuple type `(T1..Tn)` of arity N is laid out
1313
"as if" there were a corresponding tuple struct declared in libcore:
1414

15-
```rust
15+
```rust,ignore
1616
#[repr(Rust)]
1717
struct TupleN<P1..Pn:?Sized>(P1..Pn);
1818
```
@@ -50,7 +50,7 @@ Some related discussion:
5050

5151
Structs come in two principle varieties:
5252

53-
```rust
53+
```rust,ignore
5454
// Structs with named fields
5555
struct Foo { f1: T1, .., fn: Tn }
5656
@@ -61,7 +61,7 @@ struct Foo(T1, .., Tn);
6161
In terms of their layout, tuple structs can be understood as
6262
equivalent to a named struct with fields named `0..n-1`:
6363

64-
```rust
64+
```rust,ignore
6565
struct Foo {
6666
0: T1,
6767
...
@@ -283,7 +283,7 @@ void some_function(uint32_t value) { .. }
283283
It is **incorrect** to pass in a struct as that value, even if that
284284
struct is `#[repr(C)`] and has only one field:
285285
286-
```rust
286+
```rust,ignore
287287
#[repr(C)]
288288
struct Foo { x: u32 }
289289
@@ -390,5 +390,3 @@ proposal (and -- further -- it does not match our existing behavior):
390390
thread](https://github.com/rust-rfcs/unsafe-code-guidelines/pull/31#discussion_r224955817)).
391391
- Many people would prefer the name ordering to be chosen for
392392
"readability" and not optimal layout.
393-
394-
## Footnotes

Diff for: reference/src/layout/unions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ not to change until an RFC ratifies them.
99
The only degree of freedom the compiler has when computing the layout of a union
1010
like
1111

12-
```rust
12+
```rust,ignore
1313
union U { f1: T1, f2: T2 }
1414
```
1515

Diff for: reference/src/optimizations/immutable_memory.md

-1
This file was deleted.

Diff for: reference/src/optimizations/return_value_optimization.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
We should turn
22

3-
```rust
3+
```rust,ignore
44
// y unused
55
let mut x = f();
66
g(&mut x);
@@ -10,7 +10,7 @@ y = x;
1010

1111
into
1212

13-
```rust
13+
```rust,ignore
1414
y = f();
1515
g(&mut y);
1616
```

Diff for: reference/src/representation.md

-23
This file was deleted.

0 commit comments

Comments
 (0)