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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Types and traits for marking the scope of a type (i.e. whether an item is axis-scope or device-scope).
//!
//! The scope of an item is determined by whether it implements [`AxisScope`] or [`DeviceScope`].
//!
//! In some cases, types/functions need to change the required scope of their parameters based on a type
//! parameter. In such cases, the [`RequiresAxisScope`] and [`RequiresDeviceScope`] types can be used in
//! conjunction with either of the [`RequiredScopeSatisfiedBy`] or [`SatisfiesRequiredScope`] trait bounds:
//!
//! ```
//! # use zproto::ascii::chain::scope::*;
//! struct Foo<R>(std::marker::PhantomData<R>);
//! impl<R> Foo<R> {
//! fn bar<T: SatisfiesRequiredScope<R>>(_: T) { /* ... */ }
//! }
//!
//! struct AxisThing;
//! impl AxisScope for AxisThing {};
//!
//! struct DeviceThing;
//! impl DeviceScope for DeviceThing {};
//!
//! Foo::<RequiresAxisScope>::bar(AxisThing); // OK
//! // Foo::<RequiresAxisScope>::bar(DeviceThing); // Compilation error.
//! // Foo::<RequiresDeviceScope>::bar(AxisThing); // Compilation error.
//! Foo::<RequiresDeviceScope>::bar(DeviceThing); // OK
//! ```
// It may be tempting to replace the scope requirement types and associated traits with one trait,
// something like:
//
// ```
// pub trait SameScope<A, B> {}
// ```
//
// And then implement it for all types A and B where both types implement the same scope trait.
// However, this will result in conflicting implementations,
//
// ```
// impl<A, B> SameScope<A, B> for A where A: DeviceScope, B: DeviceScope {}
// impl<A, B> SameScope<A, B> for A where A: AxisScope, B: AxisScope {} // conflicting
// implementation
// ```
//
// To avoid this, the traits need to be implement on concrete types, hence the Requires*Scope
// types.
/// A marker trait indicating that a type represents an axis-scope item.
/// A marker trait indicating that a type represents a device-scope item.
/// A marker type indicating a type requires other types to satisfy an [`AxisScope`] bound.
///
/// To fully achieve this behaviour, it must be used in conjunction with one of
/// [`RequiredScopeSatisfiedBy`] or [`SatisfiesRequiredScope`]. See the [module] level
/// documentation for details.
///
/// [module]: crate::ascii::chain::scope
/// A marker type indicating a type requires other types to satisfy a [`DeviceScope`] bounds.
///
/// To fully achieve this behaviour, it must be used in conjunction with one of
/// [`RequiredScopeSatisfiedBy`] or [`SatisfiesRequiredScope`]. See the [module] level
/// documentation for details.
///
/// [module]: crate::ascii::chain::scope
/// A marker trait indicating a type indicating a scope requirement is satisfied by the type `T`.
///
/// This allows the scope requirement to be defined by a type parameter, otherwise the
/// [`AxisScope`] or [`DeviceScope`] traits should be used as a bound directly.
///
/// ```
/// # use zproto::ascii::chain::scope::RequiredScopeSatisfiedBy;
/// # fn foo<T, R>(_: T, _: R)
/// where
/// R: RequiredScopeSatisfiedBy<T>
/// // Read as "The scope required by type R is satisfied by type T," where R must be
/// // either RequiresAxisScope or RequiresDeviceScope.
/// # {}
/// ```
///
/// The inverse bound is defined by [`SatisfiesRequiredScope`].
///
/// See the [module] level documentation for more details.
///
/// [module]: crate::ascii::chain::scope
/// A marker trait indicating a type satisfies the scope requirement of `T`, which must be either
/// [`RequiresAxisScope`] and [`RequiresDeviceScope`].
///
/// This allows the scope requirement to be defined by a type parameter, otherwise the
/// [`AxisScope`] or [`DeviceScope`] traits should be used as a bound directly.
///
/// ```
/// # use zproto::ascii::chain::scope::SatisfiesRequiredScope;
/// # fn foo<T, R>(_: T, _: R)
/// where
/// T: SatisfiesRequiredScope<R>
/// // Read as "T satisfies the scope required by type R," where R must be either
/// // RequiresScopeAxis or RequiresDeviceAxis.
/// # {}
/// ```
///
/// The inverse bound is defined by [`RequiredScopeSatisfiedBy`].
///
/// See the [module] level documentation for more details.
///
/// [module]: crate::ascii::chain::scope