diff --git a/src/codecs/iri/mod.rs b/src/codecs/iri/mod.rs index 52151dd..f8ee73c 100644 --- a/src/codecs/iri/mod.rs +++ b/src/codecs/iri/mod.rs @@ -15,7 +15,7 @@ pub use did::*; mod data; pub use data::*; -pub trait IriCodec { +pub trait IriCodec: Send + Sync { fn encode(&self, suffix: &str) -> Result, EncodeError>; fn decode(&self, array: &[CborValue]) -> Result; diff --git a/src/codecs/mod.rs b/src/codecs/mod.rs index 7773947..b477cff 100644 --- a/src/codecs/mod.rs +++ b/src/codecs/mod.rs @@ -23,7 +23,7 @@ pub use xsd_date::*; mod xsd_date_time; pub use xsd_date_time::*; -pub trait TypeCodec { +pub trait TypeCodec: Send + Sync { fn encode( &self, state: &TransformerState, diff --git a/src/decode/mod.rs b/src/decode/mod.rs index 4eadeef..474c02b 100644 --- a/src/decode/mod.rs +++ b/src/decode/mod.rs @@ -12,14 +12,14 @@ use iref::{IriBuf, IriRef, IriRefBuf}; /// Decoding options. #[derive(Debug, Default)] -pub struct DecodeOptions<'a> { +pub struct DecodeOptions { // /// Map associating JSON-LD context URLs to CBOR-LD (integer) identifiers. // pub context_map: IdMap, /// Datatype codecs. pub codecs: Codecs, /// Tables. - pub default_tables: Cow<'a, Tables>, + pub default_tables: Cow<'static, Tables>, } /// Decodes a CBOR-LD document using the given JSON-LD context loader and the @@ -36,7 +36,7 @@ pub async fn decode( pub async fn decode_with( cbor_ld_document: &CborValue, loader: impl json_ld::Loader, - options: DecodeOptions<'_>, + options: DecodeOptions, ) -> Result { match cbor_ld_document { CborValue::Tag(tag, value) => { @@ -79,24 +79,24 @@ pub async fn decode_from_bytes( pub async fn decode_from_bytes_with( bytes: &[u8], loader: impl json_ld::Loader, - options: DecodeOptions<'_>, + options: DecodeOptions, ) -> Result { let cbor_ld_document = ciborium::from_reader(bytes)?; decode_with(&cbor_ld_document, loader, options).await } /// CBOR-LD decoder. -pub struct Decoder<'a, L> { +pub struct Decoder { loader: L, - state: TransformerState<'a>, + state: TransformerState, } -impl<'a, L> Decoder<'a, L> { +impl Decoder { pub fn new( loader: L, // application_context_map: IdMap, codecs: Codecs, - tables: Cow<'a, Tables>, + tables: Cow<'static, Tables>, ) -> Self { Self { loader, @@ -108,7 +108,7 @@ impl<'a, L> Decoder<'a, L> { } } -impl<'a, L> Decoder<'a, L> +impl Decoder where L: json_ld::Loader, { @@ -126,7 +126,7 @@ where } } -impl<'t, L> Transformer<'t> for Decoder<'t, L> +impl Transformer for Decoder where L: json_ld::Loader, { @@ -210,7 +210,7 @@ where .map(|v| JsonValue::String(v.into())) } - fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'t>, &mut Self::Loader) { + fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader) { (&mut self.state, &mut self.loader) } @@ -270,3 +270,22 @@ where } } } + +#[cfg(test)] +mod tests { + use std::future::Future; + + use super::{decode, decode_from_bytes}; + + fn assert_send(_: impl Send + Future) {} + + #[test] + fn decode_is_send() { + assert_send(decode(&ciborium::Value::Null, json_ld::NoLoader)) + } + + #[test] + fn decode_from_bytes_is_send() { + assert_send(decode_from_bytes(b"", json_ld::NoLoader)) + } +} diff --git a/src/encode/mod.rs b/src/encode/mod.rs index 21ee274..8cdcadb 100644 --- a/src/encode/mod.rs +++ b/src/encode/mod.rs @@ -11,12 +11,12 @@ pub use error::*; /// Encoding options. #[derive(Debug, Default)] -pub struct EncodeOptions<'a> { +pub struct EncodeOptions { /// Compression mode. pub compression_mode: CompressionMode, /// Default compression tables. - pub default_table: Cow<'a, Tables>, + pub default_table: Cow<'static, Tables>, // /// Map associating JSON-LD context URLs to CBOR-LD (integer) identifiers. // pub context_map: IdMap, @@ -38,7 +38,7 @@ pub async fn encode( pub async fn encode_with( json_ld_document: &json_ld::syntax::Value, loader: impl json_ld::Loader, - options: EncodeOptions<'_>, + options: EncodeOptions, ) -> Result { let cbor_value = match options.compression_mode { CompressionMode::Uncompressed => { @@ -77,7 +77,7 @@ pub async fn encode_to_bytes( pub async fn encode_to_bytes_with( json_ld_document: &json_ld::syntax::Value, loader: impl json_ld::Loader, - options: EncodeOptions<'_>, + options: EncodeOptions, ) -> Result, EncodeError> { encode_with(json_ld_document, loader, options) .await @@ -90,13 +90,13 @@ pub fn cbor_into_bytes(cbor: CborValue) -> Vec { bytes } -pub struct Encoder<'a, L> { +pub struct Encoder { loader: L, - state: TransformerState<'a>, + state: TransformerState, } -impl<'a, L> Encoder<'a, L> { - pub fn new(loader: L, codecs: Codecs, tables: Cow<'a, Tables>) -> Self { +impl Encoder { + pub fn new(loader: L, codecs: Codecs, tables: Cow<'static, Tables>) -> Self { Self { loader, state: TransformerState::new(codecs, tables), @@ -104,7 +104,7 @@ impl<'a, L> Encoder<'a, L> { } } -impl<'a, L> Encoder<'a, L> +impl Encoder where L: json_ld::Loader, { @@ -123,7 +123,7 @@ where } } -impl<'a, L> Transformer<'a> for Encoder<'a, L> +impl Transformer for Encoder where L: json_ld::Loader, { @@ -201,7 +201,7 @@ where self.encode_vocab_term(active_context, value) } - fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'a>, &mut Self::Loader) { + fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader) { (&mut self.state, &mut self.loader) } @@ -257,3 +257,25 @@ where } } } + +#[cfg(test)] +mod tests { + use std::future::Future; + + use super::{encode, encode_to_bytes}; + + fn assert_send(_: impl Send + Future) {} + + #[test] + fn encode_is_send() { + assert_send(encode(&json_ld::syntax::Value::Null, json_ld::NoLoader)) + } + + #[test] + fn encode_to_bytes_is_send() { + assert_send(encode_to_bytes( + &json_ld::syntax::Value::Null, + json_ld::NoLoader, + )) + } +} diff --git a/src/transform.rs b/src/transform.rs index e38944e..d8a8a50 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -164,7 +164,7 @@ pub struct ExpectedObject; pub struct InvalidTypeKind; -pub trait Transformer<'t> { +pub trait Transformer { type Input: TransformedValue; type Output: TransformedValue; @@ -222,7 +222,7 @@ pub trait Transformer<'t> { value: &Self::Input, ) -> Result; - fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'t>, &mut Self::Loader); + fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader); #[allow(async_fn_in_trait)] async fn process_global_context<'c>( @@ -517,18 +517,18 @@ fn is_alias_with_def(key: &str, def: Option, keyword: Keyword }) } -pub struct TransformerState<'a> { +pub struct TransformerState { // pub context_map: IdMap, pub allocator: IdAllocator, pub codecs: Codecs, - pub tables: Cow<'a, Tables>, + pub tables: Cow<'static, Tables>, } -impl<'a> TransformerState<'a> { +impl TransformerState { pub fn new( // context_map: IdMap, codecs: Codecs, - tables: Cow<'a, Tables>, + tables: Cow<'static, Tables>, ) -> Self { Self { // context_map,