intovec/
lib.rs

1#![deny(missing_docs)]
2#![deny(warnings)]
3
4//! Convert types into a Vec, avoiding copies when possible.
5
6use std::borrow::ToOwned;
7
8/// Anything convertible into a Vec with or without copies, but avoiding
9/// them if possible.
10pub trait IntoVec<T> {
11    /// Convert Self into a Vec
12    #[inline]
13    fn into_vec(self) -> Vec<T>;
14}
15
16impl<T> IntoVec<T> for Vec<T> {
17    #[inline]
18    fn into_vec(self) -> Vec<T> { self }
19}
20
21impl<'a, T: Clone> IntoVec<T> for &'a [T] {
22    #[inline]
23    fn into_vec(self) -> Vec<T> { self.to_vec() }
24}
25
26impl<T> IntoVec<T> for Box<[T]> {
27    #[inline]
28    fn into_vec(self) -> Vec<T> {
29        std::slice::SliceExt::into_vec(self)
30    }
31}
32
33impl IntoVec<u8> for String {
34    #[inline]
35    fn into_vec(self) -> Vec<u8> { self.into_bytes() }
36}
37
38impl<'a> IntoVec<u8> for &'a str {
39    #[inline]
40    fn into_vec(self) -> Vec<u8> { self.as_bytes().into_vec() }
41}
42
43/// Anything convertible into a String with or without copies, but avoiding
44/// them if possible.
45pub trait IntoString {
46    /// Convert Self into a String
47    fn into_string(self) -> String;
48}
49
50impl IntoString for String {
51    #[inline]
52    fn into_string(self) -> String { self }
53}
54
55impl<'a> IntoString for &'a str {
56    #[inline]
57    fn into_string(self) -> String {
58        self.to_owned()
59    }
60}
61