]>
git.proxmox.com Git - rustc.git/blob - vendor/icu_provider/src/buf.rs
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 ).
5 //! Traits for data providers that produce opaque buffers.
9 /// [`DataMarker`] for raw buffers. Returned by [`BufferProvider`].
11 /// The data is expected to be deserialized before it can be used; see
12 /// [`DataPayload::into_deserialized`].
13 #[allow(clippy::exhaustive_structs)] // marker type
14 pub struct BufferMarker
;
16 impl DataMarker
for BufferMarker
{
17 type Yokeable
= &'
static [u8];
20 /// A data provider that returns opaque bytes.
22 /// Generally, these bytes are expected to be deserializable with Serde. To get an object
23 /// implementing [`DataProvider`] via Serde, use [`as_deserializing()`], which requires
24 /// enabling at least one of the Serde features.
26 /// Along with [`DataProvider`], this is one of the two foundational traits in this crate.
28 /// [`BufferProvider`] can be made into a trait object. It is used over FFI.
33 /// # #[cfg(feature = "deserialize_json")] {
34 /// use icu_locid::locale;
35 /// use icu_provider::hello_world::*;
36 /// use icu_provider::prelude::*;
38 /// let buffer_provider = HelloWorldProvider.into_json_provider();
40 /// let data_provider = buffer_provider.as_deserializing();
42 /// let german_hello_world: DataPayload<HelloWorldV1Marker> = data_provider
43 /// .load(DataRequest {
44 /// locale: &locale!("de").into(),
45 /// metadata: Default::default(),
47 /// .expect("Loading should succeed")
49 /// .expect("Data should be present");
51 /// assert_eq!("Hallo Welt", german_hello_world.get().message);
55 /// [`as_deserializing()`]: AsDeserializingBufferProvider::as_deserializing
56 pub trait BufferProvider
{
57 /// Loads a [`DataPayload`]`<`[`BufferMarker`]`>` according to the key and request.
62 ) -> Result
<DataResponse
<BufferMarker
>, DataError
>;
65 impl<T
: BufferProvider
+ ?Sized
> BufferProvider
for alloc
::boxed
::Box
<T
> {
70 ) -> Result
<DataResponse
<BufferMarker
>, DataError
> {
71 (**self).load_buffer(key
, req
)
75 /// An enum expressing all Serde formats known to ICU4X.
76 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
77 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
79 pub enum BufferFormat
{
80 /// Serialize using JavaScript Object Notation (JSON).
82 /// Serialize using Bincode version 1.
84 /// Serialize using Postcard version 0.7.
89 /// Returns an error if the buffer format is not enabled.
90 pub fn check_available(&self) -> Result
<(), DataError
> {
92 #[cfg(feature = "deserialize_json")]
93 BufferFormat
::Json
=> Ok(()),
95 #[cfg(feature = "deserialize_bincode_1")]
96 BufferFormat
::Bincode1
=> Ok(()),
98 #[cfg(feature = "deserialize_postcard_1")]
99 BufferFormat
::Postcard1
=> Ok(()),
101 // Allowed for cases in which all features are enabled
102 #[allow(unreachable_patterns)]
103 _
=> Err(DataErrorKind
::UnavailableBufferFormat(*self).into_error()),