im/
fakepool.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5#![allow(dead_code)]
6
7use std::marker::PhantomData;
8use std::ops::Deref;
9use std::rc::Rc as RRc;
10use std::sync::Arc as RArc;
11
12use crate::nodes::chunk::Chunk;
13
14pub(crate) trait PoolDefault: Default {}
15pub(crate) trait PoolClone: Clone {}
16
17impl<A> PoolDefault for Chunk<A> {}
18impl<A> PoolClone for Chunk<A> where A: Clone {}
19
20pub(crate) struct Pool<A>(PhantomData<A>);
21
22impl<A> Pool<A> {
23    pub(crate) fn new(_size: usize) -> Self {
24        Pool(PhantomData)
25    }
26
27    pub(crate) fn get_pool_size(&self) -> usize {
28        0
29    }
30
31    pub(crate) fn fill(&self) {}
32}
33
34impl<A> Clone for Pool<A> {
35    fn clone(&self) -> Self {
36        Self::new(0)
37    }
38}
39
40// Rc
41
42#[derive(Default)]
43pub(crate) struct Rc<A>(RRc<A>);
44
45impl<A> Rc<A> {
46    #[inline(always)]
47    pub(crate) fn default(_pool: &Pool<A>) -> Self
48    where
49        A: PoolDefault,
50    {
51        Self(Default::default())
52    }
53
54    #[inline(always)]
55    pub(crate) fn new(_pool: &Pool<A>, value: A) -> Self {
56        Rc(RRc::new(value))
57    }
58
59    #[inline(always)]
60    pub(crate) fn clone_from(_pool: &Pool<A>, value: &A) -> Self
61    where
62        A: PoolClone,
63    {
64        Rc(RRc::new(value.clone()))
65    }
66
67    #[inline(always)]
68    pub(crate) fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
69    where
70        A: PoolClone,
71    {
72        RRc::make_mut(&mut this.0)
73    }
74
75    #[inline(always)]
76    pub(crate) fn ptr_eq(left: &Self, right: &Self) -> bool {
77        RRc::ptr_eq(&left.0, &right.0)
78    }
79
80    pub(crate) fn unwrap_or_clone(this: Self) -> A
81    where
82        A: PoolClone,
83    {
84        RRc::try_unwrap(this.0).unwrap_or_else(|r| (*r).clone())
85    }
86}
87
88impl<A> Clone for Rc<A> {
89    #[inline(always)]
90    fn clone(&self) -> Self {
91        Rc(self.0.clone())
92    }
93}
94
95impl<A> Deref for Rc<A> {
96    type Target = A;
97    #[inline(always)]
98    fn deref(&self) -> &Self::Target {
99        self.0.deref()
100    }
101}
102
103impl<A> PartialEq for Rc<A>
104where
105    A: PartialEq,
106{
107    #[inline(always)]
108    fn eq(&self, other: &Self) -> bool {
109        **self == **other
110    }
111}
112
113impl<A> Eq for Rc<A> where A: Eq {}
114
115impl<A> std::fmt::Debug for Rc<A>
116where
117    A: std::fmt::Debug,
118{
119    #[inline(always)]
120    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
121        self.0.fmt(f)
122    }
123}
124
125// Arc
126
127#[derive(Default)]
128pub(crate) struct Arc<A>(RArc<A>);
129
130impl<A> Arc<A> {
131    #[inline(always)]
132    pub(crate) fn default(_pool: &Pool<A>) -> Self
133    where
134        A: PoolDefault,
135    {
136        Self(Default::default())
137    }
138
139    #[inline(always)]
140    pub(crate) fn new(_pool: &Pool<A>, value: A) -> Self {
141        Self(RArc::new(value))
142    }
143
144    #[inline(always)]
145    pub(crate) fn clone_from(_pool: &Pool<A>, value: &A) -> Self
146    where
147        A: PoolClone,
148    {
149        Self(RArc::new(value.clone()))
150    }
151
152    #[inline(always)]
153    pub(crate) fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
154    where
155        A: PoolClone,
156    {
157        RArc::make_mut(&mut this.0)
158    }
159
160    #[inline(always)]
161    pub(crate) fn ptr_eq(left: &Self, right: &Self) -> bool {
162        RArc::ptr_eq(&left.0, &right.0)
163    }
164
165    pub(crate) fn unwrap_or_clone(this: Self) -> A
166    where
167        A: PoolClone,
168    {
169        RArc::try_unwrap(this.0).unwrap_or_else(|r| (*r).clone())
170    }
171}
172
173impl<A> Clone for Arc<A> {
174    #[inline(always)]
175    fn clone(&self) -> Self {
176        Self(self.0.clone())
177    }
178}
179
180impl<A> Deref for Arc<A> {
181    type Target = A;
182    #[inline(always)]
183    fn deref(&self) -> &Self::Target {
184        self.0.deref()
185    }
186}
187
188impl<A> PartialEq for Arc<A>
189where
190    A: PartialEq,
191{
192    #[inline(always)]
193    fn eq(&self, other: &Self) -> bool {
194        **self == **other
195    }
196}
197
198impl<A> Eq for Arc<A> where A: Eq {}
199
200impl<A> std::fmt::Debug for Arc<A>
201where
202    A: std::fmt::Debug,
203{
204    #[inline(always)]
205    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
206        self.0.fmt(f)
207    }
208}