rune_alloc/vec/
spec_extend.rs

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
#[cfg(rune_nightly)]
use core::slice;

use crate::alloc::Allocator;
#[cfg(rune_nightly)]
use crate::clone::{TryClone, TryCopy};
use crate::error::Error;

#[cfg(rune_nightly)]
use super::IntoIter;
use super::Vec;

// Specialization trait used for Vec::extend
pub(super) trait SpecExtend<T, I> {
    fn spec_extend(&mut self, iter: I) -> Result<(), Error>;
}

impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
where
    I: Iterator<Item = T>,
{
    default_fn! {
        fn spec_extend(&mut self, iter: I) -> Result<(), Error> {
            for value in iter {
                self.try_push(value)?;
            }

            Ok(())
        }
    }
}

#[cfg(rune_nightly)]
impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
    fn spec_extend(&mut self, mut iterator: IntoIter<T>) -> Result<(), Error> {
        unsafe {
            self.try_append_elements(iterator.as_slice() as _)?;
        }
        iterator.forget_remaining_elements();
        Ok(())
    }
}

#[cfg(rune_nightly)]
impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for Vec<T, A>
where
    I: Iterator<Item = &'a T>,
    T: TryClone,
{
    default fn spec_extend(&mut self, iterator: I) -> Result<(), Error> {
        for value in iterator {
            self.try_push(value.try_clone()?)?;
        }

        Ok(())
    }
}

#[cfg(rune_nightly)]
impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
where
    T: TryCopy,
{
    fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) -> Result<(), Error> {
        let slice = iterator.as_slice();

        unsafe {
            self.try_append_elements(slice)?;
        }

        Ok(())
    }
}