fyrox_resource/
entry.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Resource manager timed entry. It holds strong reference for a resource and a simple timer
22//! variable. When someone uses a resource, the timer variable is reset to default resource
23//! lifetime. Timer gradually decreases its value and once it reaches zero, the entry is deleted.
24//! The inner resource might still be in use (have a strong reference to it), the resource data
25//! will be deleted once no one uses the resource.
26
27use std::ops::{Deref, DerefMut};
28
29/// Lifetime of orphaned resource in seconds (with only one strong ref which is resource manager itself)
30pub const DEFAULT_RESOURCE_LIFETIME: f32 = 60.0;
31
32/// Resource container with fixed TTL (time-to-live). Resource will be removed
33/// (and unloaded) if there were no other strong references to it in given time
34/// span.
35pub struct TimedEntry<T> {
36    /// Payload of entry.
37    pub value: T,
38    /// Time to live in seconds.
39    pub time_to_live: f32,
40}
41
42impl<T> Deref for TimedEntry<T> {
43    type Target = T;
44
45    fn deref(&self) -> &Self::Target {
46        &self.value
47    }
48}
49
50impl<T> DerefMut for TimedEntry<T> {
51    fn deref_mut(&mut self) -> &mut Self::Target {
52        &mut self.value
53    }
54}
55
56impl<T> Default for TimedEntry<T>
57where
58    T: Default,
59{
60    fn default() -> Self {
61        Self {
62            value: Default::default(),
63            time_to_live: DEFAULT_RESOURCE_LIFETIME,
64        }
65    }
66}
67
68impl<T> Clone for TimedEntry<T>
69where
70    T: Clone,
71{
72    fn clone(&self) -> Self {
73        Self {
74            value: self.value.clone(),
75            time_to_live: self.time_to_live,
76        }
77    }
78}
79
80#[cfg(test)]
81mod test {
82    use super::*;
83
84    #[test]
85    fn timed_entry_default() {
86        let t: TimedEntry<_> = TimedEntry::<u32>::default();
87
88        assert_eq!(t.value, 0);
89        assert_eq!(t.time_to_live, DEFAULT_RESOURCE_LIFETIME);
90    }
91
92    #[test]
93    fn timed_entry_deref() {
94        let t = TimedEntry {
95            value: 42,
96            ..Default::default()
97        };
98
99        assert_eq!(t.deref(), &42);
100    }
101
102    #[test]
103    fn timed_entry_deref_mut() {
104        let mut t = TimedEntry {
105            value: 42,
106            ..Default::default()
107        };
108
109        assert_eq!(t.deref_mut(), &mut 42);
110    }
111
112    #[test]
113    fn timed_entry_clone() {
114        let t = TimedEntry {
115            value: 42,
116            time_to_live: 15.0,
117        };
118        let t2 = t.clone();
119
120        assert_eq!(t.value, t2.value);
121        assert_eq!(t.time_to_live, t2.time_to_live);
122    }
123}