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
// Copyright (C) 2022 Parity Technologies (UK) Ltd. (admin@parity.io)
// This file is a part of the scale-value crate.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::TypeId;
/// This represents a compact encoded type.
pub struct Compact<'b, 'c, T> {
val: T,
locations: &'c [CompactLocation<'b>],
}
impl<'b, 'c, T: Copy> Compact<'b, 'c, T> {
pub(crate) fn new(val: T, locations: &'c [CompactLocation<'b>]) -> Compact<'b, 'c, T> {
Compact { val, locations }
}
/// Return the value that was compact-encoded
pub fn value(&self) -> T {
self.val
}
/// Compact values can be nested inside named or unnamed fields in structs.
/// This provides back a slice of
pub fn locations(&self) -> &'c [CompactLocation<'b>] {
self.locations
}
}
/// A pointer to what the compact value is contained within.
#[derive(Clone, Copy, Debug)]
pub enum CompactLocation<'b> {
/// We're in an unnamed composite (struct) with the type ID given.
UnnamedComposite(TypeId),
/// We're in a named composite (struct) with the type ID given, and the compact
/// value lives inside the field with the given name.
NamedComposite(TypeId, &'b str),
/// We're at a primitive type with the type ID given; the compact value itself.
Primitive(TypeId),
}
impl<'b> CompactLocation<'b> {
/// Return the Primitive type of this location, if one exists.
pub fn as_primitive(self) -> Option<TypeId> {
match self {
CompactLocation::Primitive(t) => Some(t),
_ => None,
}
}
}
// Default values for locations are never handed back, but they are
// stored on the StackArray in the "unused" positions. We could avoid needing
// this with some unsafe code.
impl<'a> Default for CompactLocation<'a> {
fn default() -> Self {
CompactLocation::Primitive(TypeId::default())
}
}