Skip to main content

any_vec/any_value/
lazy_clone.rs

1use core::any::TypeId;
2use crate::any_value::{AnyValue, AnyValueCloneable, AnyValueTypeless, AnyValueSizeless};
3
4/// Makes [`AnyValueCloneable`] actually [`Clone`]able.
5/// Clone underlying value on consumption.
6///
7/// Source must outlive `LazyClone`. `LazyClone` let you
8/// take element from one [`AnyVec`] and put it multiple times
9/// into another, without intermediate copies and cast to concrete type.
10/// 
11/// Can be constructed by calling [AnyValueCloneable::lazy_clone].
12///
13/// [`AnyVec`]: crate::AnyVec
14pub struct LazyClone<'a, T: AnyValueCloneable>{
15    value: &'a T
16}
17
18impl<'a, T: AnyValueCloneable> LazyClone<'a, T>{
19    #[inline]
20    pub fn new(value: &'a T) -> Self{
21        Self{value}
22    }
23}
24
25impl<'a, T: AnyValueCloneable> AnyValueSizeless for LazyClone<'a, T> {
26    type Type = T::Type;
27
28    #[inline]
29    fn as_bytes_ptr(&self) -> *const u8 {
30        self.value.as_bytes_ptr()
31    }
32
33    #[inline]
34    unsafe fn move_into<KnownType:'static /*= Unknown*/>(self, out: *mut u8, _bytes_size: usize) {
35        self.value.clone_into(out);
36    }
37}
38
39impl<'a, T: AnyValueCloneable + AnyValueTypeless> AnyValueTypeless for LazyClone<'a, T>{
40    #[inline]
41    fn size(&self) -> usize {
42        self.value.size()
43    }
44}
45
46impl<'a, T: AnyValueCloneable + AnyValue> AnyValue for LazyClone<'a, T>{
47    #[inline]
48    fn value_typeid(&self) -> TypeId {
49        self.value.value_typeid()
50    }
51}
52
53impl<'a, T: AnyValueCloneable> AnyValueCloneable for LazyClone<'a, T>{
54    #[inline]
55    unsafe fn clone_into(&self, out: *mut u8) {
56        self.value.clone_into(out);
57    }
58}
59
60impl<'a, T: AnyValueCloneable> Clone for LazyClone<'a, T>{
61    #[inline]
62    fn clone(&self) -> Self {
63        Self{value: self.value}
64    }
65}