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
#![deny(missing_docs)]
#![deny(warnings)]

//! Convert types into a Vec, avoiding copies when possible.

use std::borrow::ToOwned;

/// Anything convertible into a Vec with or without copies, but avoiding
/// them if possible.
pub trait IntoVec<T> {
    /// Convert Self into a Vec
    #[inline]
    fn into_vec(self) -> Vec<T>;
}

impl<T> IntoVec<T> for Vec<T> {
    #[inline]
    fn into_vec(self) -> Vec<T> { self }
}

impl<'a, T: Clone> IntoVec<T> for &'a [T] {
    #[inline]
    fn into_vec(self) -> Vec<T> { self.to_vec() }
}

impl<T> IntoVec<T> for Box<[T]> {
    #[inline]
    fn into_vec(self) -> Vec<T> {
        std::slice::SliceExt::into_vec(self)
    }
}

impl IntoVec<u8> for String {
    #[inline]
    fn into_vec(self) -> Vec<u8> { self.into_bytes() }
}

impl<'a> IntoVec<u8> for &'a str {
    #[inline]
    fn into_vec(self) -> Vec<u8> { self.as_bytes().into_vec() }
}

/// Anything convertible into a String with or without copies, but avoiding
/// them if possible.
pub trait IntoString {
    /// Convert Self into a String
    fn into_string(self) -> String;
}

impl IntoString for String {
    #[inline]
    fn into_string(self) -> String { self }
}

impl<'a> IntoString for &'a str {
    #[inline]
    fn into_string(self) -> String {
        self.to_owned()
    }
}