rune_alloc/vec/
spec_from_elem.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[cfg(rune_nightly)]
use core::ptr;

use crate::alloc::Allocator;
use crate::clone::TryClone;
use crate::error::Error;
#[cfg(rune_nightly)]
use crate::raw_vec::RawVec;

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

// Specialization trait used for Vec::from_elem
pub(super) trait SpecFromElem: Sized {
    fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Result<Vec<Self, A>, Error>;
}

impl<T> SpecFromElem for T
where
    T: TryClone,
{
    default_fn! {
        fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Result<Vec<Self, A>, Error> {
            let mut v = Vec::try_with_capacity_in(n, alloc)?;
            v.try_extend_with(n, elem)?;
            Ok(v)
        }
    }
}

#[cfg(rune_nightly)]
impl<T> SpecFromElem for T
where
    T: TryClone + IsZero,
{
    #[inline]
    default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Result<Vec<T, A>, Error> {
        if elem.is_zero() {
            return Ok(Vec {
                buf: RawVec::try_with_capacity_zeroed_in(n, alloc)?,
                len: n,
            });
        }

        let mut v = Vec::try_with_capacity_in(n, alloc)?;
        v.try_extend_with(n, elem)?;
        Ok(v)
    }
}

#[cfg(rune_nightly)]
impl SpecFromElem for i8 {
    #[inline]
    fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Result<Vec<i8, A>, Error> {
        if elem == 0 {
            return Ok(Vec {
                buf: RawVec::try_with_capacity_zeroed_in(n, alloc)?,
                len: n,
            });
        }

        unsafe {
            let mut v = Vec::try_with_capacity_in(n, alloc)?;
            ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
            v.set_len(n);
            Ok(v)
        }
    }
}

#[cfg(rune_nightly)]
impl SpecFromElem for u8 {
    #[inline]
    fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Result<Vec<u8, A>, Error> {
        if elem == 0 {
            return Ok(Vec {
                buf: RawVec::try_with_capacity_zeroed_in(n, alloc)?,
                len: n,
            });
        }

        unsafe {
            let mut v = Vec::try_with_capacity_in(n, alloc)?;
            ptr::write_bytes(v.as_mut_ptr(), elem, n);
            v.set_len(n);
            Ok(v)
        }
    }
}