| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! Runtime `Skeleton` implementation for more efficient deserialization. |
| 6 |
|
| 7 |
use super::reference; |
| 8 |
use crate::provider::fields::Field; |
| 9 |
use alloc::vec::Vec; |
| 10 |
use zerovec::ZeroVec; |
| 11 |
|
| 12 |
/// A skeleton that supports zero-copy deserialization. |
| 13 |
/// |
| 14 |
/// <div class="stab unstable"> |
| 15 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 16 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 17 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 18 |
/// </div> |
| 19 |
#[derive(Debug, PartialEq, Clone)] |
| 20 |
pub struct Skeleton<'data>(pub(crate) ZeroVec<'data, Field>); |
| 21 |
|
| 22 |
impl From<reference::Skeleton> for Skeleton<'_> { |
| 23 |
fn from(input: reference::Skeleton) -> Self { |
| 24 |
let fields = input.fields_iter().copied().collect::<Vec<_>>(); |
| 25 |
Self(ZeroVec::alloc_from_slice(&fields)) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl<'data> From<ZeroVec<'data, Field>> for Skeleton<'data> { |
| 30 |
fn from(input: ZeroVec<'data, Field>) -> Self { |
| 31 |
Self(input) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl core::fmt::Display for Skeleton<'_> { |
| 36 |
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 37 |
use core::fmt::Write; |
| 38 |
for field in self.0.iter() { |
| 39 |
let ch: char = field.symbol.into(); |
| 40 |
for _ in 0..field.length.to_len() { |
| 41 |
formatter.write_char(ch)?; |
| 42 |
} |
| 43 |
} |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
} |