1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::*;
use std::{
    marker::PhantomData,
};

/// Iterator for initialising potentially uninitialised `HeapArray<T>`.
pub struct InitIter<'a, T>
{
    from: &'a mut HeapArray<T>,
    current_idex: usize,
}

/// A safe wrapper to initialise potentially uninitialised data.
pub struct Init<'a, T>
{
    ptr: *mut T,
    init_ok: bool,
    _marker: PhantomData<&'a T>,
}

impl<'a, T> InitIter<'a, T>
{
    pub(crate) fn new(from: &'a mut HeapArray<T>, current_idex: usize) -> InitIter<'a, T>
    {
	InitIter {
	    from,
	    current_idex,
	}
    }

    /// Consumes the instance, zeroing all remaining bytes in the iterator.
    pub fn uninit(self)
    {
	let len = self.from.len_bytes() - (self.current_idex * HeapArray::<T>::element_size());
	if len > 0 {
	    unsafe {
		ptr::memset(self.from.as_mut_ptr().offset(self.current_idex as isize) as *mut u8, 0, len);
	    }
	}
    }
}

impl<'a, T> Iterator for InitIter<'a, T>
{
    type Item = Init<'a, T>;

    fn next(&mut self) -> Option<Self::Item>
    {
	if self.current_idex >= self.from.len() {
	    None
	} else {
	    self.current_idex+=1;
	    unsafe {
		Some(Init{
		    ptr: self.from.as_mut_ptr().offset((self.current_idex as isize)-1),
		    init_ok: false,
		    _marker: PhantomData,
		})
	    }
	}
    }
}

pub trait InitIterExt<T>
{

    /// Fill the rest of the iterator with a `clone()`d value.
    fn fill(self, value: T) where T: Clone;

    /// Fill the rest of the iterator with output from a function.
    fn fill_with<F>(self, func: F) where F: FnMut() -> T;

    /// Fill the rest of the iterator with `default()`
    fn fill_default(self) where T: Default;
}

impl<'a, T,I> InitIterExt<T> for I
where I: Iterator<Item=Init<'a, T>>,
      T: 'a
{
    fn fill(self, value: T)
    where T:Clone
    {
	for mut x in self
	{
	    if !x.is_init() {
		x.put(value.clone());
	    }
	}
    }
    fn fill_with<F>(self, mut func: F)
    where F: FnMut() -> T
    {
	for mut x in self
	{
	    if !x.is_init() {
		x.put(func());
	    }
	}
    }
    fn fill_default(self)
    where T:Default
    {
	for mut x in self
	{
	    if !x.is_init() {
		x.put(Default::default());
	    }
	}
    }
}

impl<'a, T> Init<'a, T>
{

    /// Has the value been set with `put()` or `assume_init()` yet?
    pub fn is_init(&self) -> bool
    {
	self.init_ok
    }

    /// Assume the value has been initialised.
    pub unsafe fn assume_init(&mut self)
    {
	self.init_ok = true;
    }

    /// Initialise or reset the value and then return a mutable reference to it.
    pub fn put(&mut self, value: T) -> &mut T
    {
	if self.init_ok {
	    unsafe {
		*self.ptr = value;	
		return &mut (*self.ptr);
	    }
	}
	self.init_ok = true;
	unsafe {
	    ptr::put(self.ptr, value);
	    &mut (*self.ptr)
	}
    }

    /// Get a reference to the value if it has been initialised.
    pub fn get(&self) -> Option<&T>
    {
	unsafe {
	    if self.init_ok {
		Some(& (*self.ptr))
	    } else {
		None
	    }
	}
    }

    /// Get a mutable reference to the value if it has been initialised.
    pub fn get_mut(&mut self) -> Option<&mut T>
    {
	unsafe {
	    if self.init_ok {
		Some(&mut (*self.ptr))
	    } else {
		None
	    }
	}
    }
}