amplify/strategy.rs
1// Rust language amplification library providing multiple generic trait
2// implementations, type wrappers, derive macros and other language enhancements
3//
4// Written in 2019-2020 by
5// Martin Habovstiak <martin.habovstiak@gmail.com>
6// Dr. Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// To the extent possible under law, the author(s) have dedicated all
9// copyright and related and neighboring rights to this software to
10// the public domain worldwide. This software is distributed without
11// any warranty.
12//
13// You should have received a copy of the MIT License
14// along with this software.
15// If not, see <https://opensource.org/licenses/MIT>.
16
17//! This is a trick for rust compiler helping to distinguish types implementing
18//! mutually-exclusive traits (required until negative trait impls will be
19//! there) Implemented after concept by Martin Habovštiak
20//! <martin.habovstiak@gmail.com>
21//!
22//! The module proposes **generic implementation strategies**, which allow
23//! multiple generic trait implementations.
24//!
25//! Implementing trait for a generic type ("blanket implementation") more than
26//! once (applies both for local and foreign traits) - or implement foreign
27//! trait for a concrete type where there is some blanket implementation in the
28//! upstream. The solution is to use special pattern by @Kixunil. I use it
29//! widely and have a special helper type in
30//! [`src/strategy.rs`]()src/strategy.rs module.
31//!
32//! With that helper type you can write the following code, which will provide
33//! you with efficiently multiple blanket implementations of some trait
34//! `SampleTrait`:
35//!
36//! ```
37//! pub trait SampleTrait {
38//! fn sample_trait_method(&self);
39//! }
40//!
41//! // Define strategies, one per specific implementation that you need,
42//! // either blanket or concrete
43//! pub struct StrategyA;
44//! pub struct StrategyB;
45//! pub struct StrategyC;
46//!
47//! // Define a single marker type
48//! pub trait Strategy {
49//! type Strategy;
50//! }
51//!
52//! // Do a single blanket implementation using Holder and Strategy marker trait
53//! impl<T> SampleTrait for T
54//! where
55//! T: Strategy,
56//! for<'a> amplify::Holder<&'a T, T::Strategy>: SampleTrait,
57//! {
58//! // Do this for each of sample trait methods:
59//! fn sample_trait_method(&self) {
60//! amplify::Holder::new(self).sample_trait_method()
61//! }
62//! }
63//!
64//! // Do this type of implementation for each of the strategies
65//! impl<'a, T> SampleTrait for amplify::Holder<&'a T, StrategyA>
66//! where
67//! T: Strategy,
68//! {
69//! fn sample_trait_method(&self) {
70//! /* write your implementation-specific code here accessing type data,
71//! when needed, via `self.as_inner()` */
72//! }
73//! }
74//!
75//! # pub struct ConcreteTypeA;
76//! // Finally, apply specific implementation strategy to a concrete type
77//! // (or do it in a blanket generic way) as a marker:
78//! impl Strategy for ConcreteTypeA {
79//! type Strategy = StrategyA;
80//! }
81//! ```
82
83use ::core::marker::PhantomData;
84
85/// Helper type allowing implementation of trait object for generic types
86/// multiple times. In practice this type is never used
87pub struct Holder<T, S>(T, PhantomData<S>);
88impl<T, S> Holder<T, S> {
89 /// Wraps type into a holder to apply necessary blanked implementations.
90 #[inline]
91 pub fn new(val: T) -> Self {
92 Self(val, PhantomData)
93 }
94
95 /// Returns a reference to the wrapped type.
96 #[inline]
97 pub fn as_type(&self) -> &T {
98 &self.0
99 }
100}