hime_redist/utils/
mod.rs

1/*******************************************************************************
2 * Copyright (c) 2017 Association Cénotélie (cenotelie.fr)
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Lesser General Public License as
5 * published by the Free Software Foundation, either version 3
6 * of the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General
14 * Public License along with this program.
15 * If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18//! Module for utility APIs
19
20use core::ops::{Deref, DerefMut};
21
22pub mod biglist;
23pub mod bin;
24
25/// Represents a reference to a structure that can be either mutable or immutable
26pub enum EitherMut<'a, T: 'a> {
27    /// The immutable reference
28    Immutable(&'a T),
29    /// The mutable reference
30    Mutable(&'a mut T),
31}
32
33impl<'a, T: 'a> Deref for EitherMut<'a, T> {
34    type Target = T;
35
36    fn deref(&self) -> &Self::Target {
37        match self {
38            EitherMut::Mutable(data) => data,
39            EitherMut::Immutable(data) => data,
40        }
41    }
42}
43
44impl<'a, T: 'a> DerefMut for EitherMut<'a, T> {
45    fn deref_mut(&mut self) -> &mut T {
46        match self {
47            EitherMut::Mutable(data) => data,
48            EitherMut::Immutable(_) => panic!("Expected a mutable reference"),
49        }
50    }
51}
52
53/// Represents a resource that is either owned,
54/// or exclusively held with a a mutable reference
55pub enum OwnOrMut<'a, T> {
56    /// The resource is directly owned
57    Owned(T),
58    /// The resource is held through a mutable reference
59    MutRef(&'a mut T),
60}
61
62impl<'a, T> Deref for OwnOrMut<'a, T> {
63    type Target = T;
64
65    fn deref(&self) -> &Self::Target {
66        match self {
67            OwnOrMut::Owned(data) => data,
68            OwnOrMut::MutRef(data) => data,
69        }
70    }
71}
72
73impl<'a, T> DerefMut for OwnOrMut<'a, T> {
74    fn deref_mut(&mut self) -> &mut Self::Target {
75        match self {
76            OwnOrMut::Owned(data) => data,
77            OwnOrMut::MutRef(data) => data,
78        }
79    }
80}