capnp/
data.rs

1// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors
2// Licensed under the MIT License:
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21
22//! Sequence of bytes.
23
24use crate::private::layout::{PointerBuilder, PointerReader};
25use crate::Result;
26
27#[derive(Copy, Clone)]
28pub struct Owned(());
29
30impl crate::traits::Owned for Owned {
31    type Reader<'a> = Reader<'a>;
32    type Builder<'a> = Builder<'a>;
33}
34
35impl crate::introspect::Introspect for Owned {
36    fn introspect() -> crate::introspect::Type {
37        crate::introspect::TypeVariant::Data.into()
38    }
39}
40
41pub type Reader<'a> = &'a [u8];
42
43pub(crate) unsafe fn reader_from_raw_parts<'a>(p: *const u8, len: u32) -> Reader<'a> {
44    ::core::slice::from_raw_parts(p, len as usize)
45}
46
47impl<'a> crate::traits::FromPointerReader<'a> for Reader<'a> {
48    fn get_from_pointer(
49        reader: &PointerReader<'a>,
50        default: Option<&'a [crate::Word]>,
51    ) -> Result<Reader<'a>> {
52        reader.get_data(default)
53    }
54}
55
56pub type Builder<'a> = &'a mut [u8];
57
58pub(crate) unsafe fn builder_from_raw_parts<'a>(p: *mut u8, len: u32) -> Builder<'a> {
59    ::core::slice::from_raw_parts_mut(p, len as usize)
60}
61
62impl<'a> crate::traits::FromPointerBuilder<'a> for Builder<'a> {
63    fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a> {
64        builder.init_data(size)
65    }
66    fn get_from_pointer(
67        builder: PointerBuilder<'a>,
68        default: Option<&'a [crate::Word]>,
69    ) -> Result<Builder<'a>> {
70        builder.get_data(default)
71    }
72}
73
74impl<'a> crate::traits::SetterInput<Owned> for Reader<'a> {
75    fn set_pointer_builder<'b>(
76        mut pointer: PointerBuilder<'b>,
77        value: Reader<'a>,
78        _canonicalize: bool,
79    ) -> Result<()> {
80        pointer.set_data(value);
81        Ok(())
82    }
83}
84
85impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
86    fn from(d: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
87        crate::dynamic_value::Reader::Data(d)
88    }
89}
90
91impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
92    fn from(d: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
93        crate::dynamic_value::Builder::Data(d)
94    }
95}