Skip to main content

irox_tools/read/
conv.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2023 IROX Contributors
3//
4
5extern crate alloc;
6use alloc::collections::VecDeque;
7use std::io::Read;
8
9/// Always returns zero
10#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
11pub struct ReadEmpty;
12impl Read for ReadEmpty {
13    fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
14        Ok(0)
15    }
16}
17
18#[derive(Default, Clone)]
19pub struct ReadAny {
20    deque: VecDeque<u8>,
21}
22
23impl Read for ReadAny {
24    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25        self.deque.read(buf)
26    }
27}
28
29impl From<String> for ReadAny {
30    fn from(value: String) -> Self {
31        ReadAny {
32            deque: VecDeque::from(Vec::from(value.as_str())),
33        }
34    }
35}
36impl From<&String> for ReadAny {
37    fn from(value: &String) -> Self {
38        ReadAny {
39            deque: VecDeque::from(Vec::from(value.as_str())),
40        }
41    }
42}
43impl From<Vec<u8>> for ReadAny {
44    fn from(value: Vec<u8>) -> Self {
45        ReadAny {
46            deque: VecDeque::from(value),
47        }
48    }
49}
50impl From<&Vec<u8>> for ReadAny {
51    fn from(value: &Vec<u8>) -> Self {
52        ReadAny {
53            deque: VecDeque::from(value.clone()),
54        }
55    }
56}