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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! The bytes package, providing access to the bytes type.

use crate::context::{ContextError, Module};
use crate::value::{ValueType, ValueTypeInfo};
use crate::vm::RawRefGuard;
use std::any::{type_name, TypeId};
use std::fmt;
use std::ops;

/// A bytes container.
#[derive(Clone)]
pub struct Bytes {
    bytes: Vec<u8>,
}

impl ops::Deref for Bytes {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.bytes
    }
}

impl fmt::Debug for Bytes {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.bytes, fmt)
    }
}

impl Bytes {
    /// Convert into inner byte array.
    pub fn into_inner(self) -> Vec<u8> {
        self.bytes
    }

    /// Construct from a byte array.
    pub fn from_bytes(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }

    /// Construct a new bytes container.
    fn new() -> Self {
        Bytes { bytes: Vec::new() }
    }

    /// Construct a new bytes container with the specified capacity.
    fn with_capacity(cap: usize) -> Self {
        Bytes {
            bytes: Vec::with_capacity(cap),
        }
    }

    /// Do something with the bytes.
    fn extend(&mut self, other: &Self) {
        self.bytes.extend(other.bytes.iter().copied());
    }

    /// Do something with the bytes.
    fn extend_str(&mut self, s: &str) {
        self.bytes.extend(s.as_bytes());
    }

    /// Get the length of the bytes collection.
    fn len(&self) -> usize {
        self.bytes.len()
    }

    /// Get the capacity of the bytes collection.
    fn capacity(&self) -> usize {
        self.bytes.capacity()
    }

    /// Get the bytes collection.
    fn clear(&mut self) {
        self.bytes.clear();
    }

    fn reserve(&mut self, additional: usize) {
        self.bytes.reserve(additional);
    }

    fn reserve_exact(&mut self, additional: usize) {
        self.bytes.reserve_exact(additional);
    }

    fn shrink_to_fit(&mut self) {
        self.bytes.shrink_to_fit();
    }

    fn pop(&mut self) -> Option<u8> {
        self.bytes.pop()
    }

    fn last(&mut self) -> Option<u8> {
        self.bytes.last().copied()
    }
}

decl_external!(Bytes);

impl<'a> crate::UnsafeFromValue for &'a [u8] {
    type Guard = RawRefGuard;

    unsafe fn unsafe_from_value(
        value: crate::ValuePtr,
        vm: &mut crate::Vm,
    ) -> Result<(Self, Self::Guard), crate::StackError> {
        let slot = value.into_external(vm)?;
        let (value, guard) = crate::Ref::unsafe_into_ref(vm.external_ref::<Bytes>(slot)?);
        Ok((value.bytes.as_slice(), guard))
    }
}

impl<'a> crate::ReflectValueType for &'a [u8] {
    fn value_type() -> ValueType {
        ValueType::External(TypeId::of::<Bytes>())
    }

    fn value_type_info() -> ValueTypeInfo {
        ValueTypeInfo::External(type_name::<Bytes>())
    }
}

/// Get the module for the bytes package.
pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::new(&["std", "bytes"]);

    module.ty(&["Bytes"]).build::<Bytes>()?;
    module.function(&["Bytes", "new"], Bytes::new)?;
    module.function(&["Bytes", "with_capacity"], Bytes::with_capacity)?;

    module.inst_fn("extend", Bytes::extend)?;
    module.inst_fn("extend_str", Bytes::extend_str)?;
    module.inst_fn("pop", Bytes::pop)?;
    module.inst_fn("last", Bytes::last)?;

    module.inst_fn("len", Bytes::len)?;
    module.inst_fn("capacity", Bytes::capacity)?;
    module.inst_fn("clear", Bytes::clear)?;
    module.inst_fn("reserve", Bytes::reserve)?;
    module.inst_fn("reserve_exact", Bytes::reserve_exact)?;
    module.inst_fn("clone", Bytes::clone)?;
    module.inst_fn("shrink_to_fit", Bytes::shrink_to_fit)?;
    Ok(module)
}