1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Code that must not compile.
//!
//! Threads cannot share an arena, since its bump pointer is not atomic:
//!
//! ```compile_fail,E0277
//! fn assert_sync<T: Sync>() {}
//! assert_sync::<rapidproto::Arena<'static>>();
//! ```
//!
//! So a thread cannot use another thread's arena through a reference:
//!
//! ```compile_fail,E0277
//! let arena = rapidproto::Arena::new();
//! std::thread::scope(|s| {
//! s.spawn(|| arena.bytes_used());
//! });
//! ```
//!
//! An arena cannot move to another thread while something borrows it:
//!
//! ```compile_fail,E0505
//! let arena = rapidproto::Arena::new();
//! let borrowed = &arena;
//! std::thread::spawn(move || drop(arena));
//! borrowed.bytes_used();
//! ```
//!
//! An `Owned` is `Send` only when its message is:
//!
//! ```compile_fail,E0277
//! use rapidproto::{Arena, ArenaDecode, DecodeError, Owned};
//!
//! struct RcMessage(std::rc::Rc<u8>);
//! struct RcDecoder;
//! // SAFETY: a test type, never decoded.
//! unsafe impl ArenaDecode for RcDecoder {
//! type Message<'a> = RcMessage;
//! fn decode_borrowed<'a>(_: &'a [u8], _: &'a Arena<'a>) -> Result<&'a RcMessage, DecodeError> {
//! unimplemented!()
//! }
//! }
//!
//! fn assert_send<T: Send>() {}
//! assert_send::<Owned<RcDecoder>>();
//! ```
//!
//! And `Sync` only when its message is:
//!
//! ```compile_fail,E0277
//! use rapidproto::{Arena, ArenaDecode, DecodeError, Owned};
//!
//! struct CellMessage(std::cell::Cell<u8>);
//! struct CellDecoder;
//! // SAFETY: a test type, never decoded.
//! unsafe impl ArenaDecode for CellDecoder {
//! type Message<'a> = CellMessage;
//! fn decode_borrowed<'a>(_: &'a [u8], _: &'a Arena<'a>) -> Result<&'a CellMessage, DecodeError> {
//! unimplemented!()
//! }
//! }
//!
//! fn assert_sync<T: Sync>() {}
//! assert_sync::<Owned<CellDecoder>>();
//! ```