intrusive_collections/link_ops.rs
1// Copyright 2020 Amari Robinson
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8/// Base trait for link operations.
9///
10/// `LinkPtr` is the representation of a link pointer.
11/// Typically this is `NonNull`, but compact representations such
12/// as `u8` or `u16` are possible.
13pub unsafe trait LinkOps {
14 /// The link pointer type.
15 type LinkPtr: Copy + Eq;
16
17 /// Attempts to acquire ownership of a link so that it can be used in an
18 /// intrusive collection.
19 ///
20 /// If this function succeeds then the intrusive collection will have
21 /// exclusive access to the link until `release_link` is called.
22 unsafe fn acquire_link(&mut self, ptr: Self::LinkPtr) -> bool;
23
24 /// Releases ownership of a link that was previously acquired with `acquire_link`.
25 ///
26 /// # Safety
27 /// An implementation of `release_link` must not panic.
28 unsafe fn release_link(&mut self, ptr: Self::LinkPtr);
29}
30
31/// The default implementation of `LinkOps` associated with a link type.
32pub trait DefaultLinkOps {
33 /// The default link operations.
34 type Ops: LinkOps + Default;
35
36 /// The associated constant that represents `Ops::default()`.
37 ///
38 /// This exists because `Default::default()` is not a constant function.
39 const NEW: Self::Ops;
40}