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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#[macro_use] extern crate erased_serde; extern crate self as vessels; extern crate alloc; pub mod channel; #[doc(inline)] pub use channel::OnTo; use channel::{Channel, Target}; pub mod format; #[doc(inline)] pub use format::{ApplyDecode, ApplyEncode}; pub mod core; pub mod kind; use kind::{ConstructResult, DeconstructResult}; pub mod reflect; pub mod replicate; use ::core::any::Any; use downcast_rs::{impl_downcast, Downcast}; use erased_serde::Serialize as ErasedSerialize; use futures::Future; use serde::{de::DeserializeOwned, Serialize}; use std::error::Error; /// Generates an implementation of `Kind` for trait objects. /// /// Annotating an object-safe trait with this macro will allow the use of /// trait objects constructed from that trait as `Kind`s. This uses the implementations /// of `Kind` for boxed `dyn Fn`s internally and therefore only functions that return /// some `Flatten` type will result in an implementation that compiles. This is intended, /// synchronous returns in an RPC system are an antipattern and this system avoids them. /// ``` /// use vessels::object; /// /// #[object] /// pub trait Object<T: Kind> { /// fn test(&self) -> Future<T>; /// } /// ``` /// The above will generate an implementation of Kind for `Box<dyn Object<T>>` where `T: Kind`. /// Generic parameters are, as thereby evidenced, supported. Functions with between zero and sixteen arguments /// not including receiver are supported where all arguments implement `Kind`. Annotated wrapper Kinds as with /// the primary derive macro are not supported, though support is planned. /// /// Associated type parameters are not permitted, they offer no advantage on trait objects as they must be /// statically described therein. Moreover, they would require additional parametrization of `Trait` which would /// come at an ergonomics cost without any benefit. pub use derive::object; /// Generates an implementation of `Kind` for a struct or enum. /// /// This macro has a number of modes of operation. /// First, it may be used in a fashion equivalent to the manner of operation of standard library derive macros. /// ``` /// use vessels::Kind; /// /// #[derive(Kind)] /// struct Person<T> { /// name: T, /// say_hello: Box<dyn Fn() -> T + Sync + Send>, /// } /// ``` /// This will generate an implementation of `Kind` for the annotated type given an extant implementation of `Kind` /// for each field of that type. There is further nuance to this mode of operation, but to explain it is best to first /// demonstrate the other primary manner of operation. /// ``` /// use vessels::{Kind, kind::using}; /// use serde::{Serialize, Deserialize} /// /// #[derive(Serialize, Deserialize)] /// struct NotKind; /// /// #[derive(Serialize, Deserialize, Kind)] /// #[kind(using::Serde)] /// struct Person { /// name: String, /// data: NotKind, /// } /// ``` /// This will generate an implementation of `Kind` for the annotated type despite `NotKind` lacking a valid implementation. /// The types, provided in `vessels::kind::using`, that provide `AsKind` trait implementations, allow for the use of an /// alternative bijection for structs and enums that implement some certain traits permitting such a thing. To finally attend /// to the additional mode of operation mentioned earlier, these `#[kind()]` annotations may be used with the initially discussed /// syntax. /// ``` /// #[derive(Kind)] /// struct Person { /// name: String, /// #[kind(using::Serde)] /// data: NotKind, /// } /// ``` /// Annotating a field of a struct or enum with `#[kind()]`, if the type provided in the attribute annotation is a valid `AsKind` /// for the type of that field, will cause the overarching derivation to use that type as a wrapper to produce a valid `Kind` bijection. /// All of the described behavior also functions for arbitrary generic parameters and when used in enums with both named and unnamed fields. /// ``` /// #[derive(Kind)] /// enum Entity<T: Kind> { /// Person { /// name: String, /// #[kind(using::Serde)] /// data: NotKind, /// }, /// UnnamedFields(#[kind(using::Serde)] NotKind, T) /// } /// ``` pub use derive::Kind; /// Generates the entry point of a vessel. /// /// ``` /// use vessels::export; /// /// export! { /// "test".to_owned() /// } /// ``` /// /// `export` wraps a block that returns a `Kind` and generates the entry point for a vessel providing that `Kind`. /// `export` should be used in a `bin` target of a crate i.e. for the default cargo configuration `main.rs`. /// No `main` function is necessary when `export` is used, in fact the presence of a `main` function will cause an /// exported vessel to fail to compile due to a symbol conflict. Finally, `export` cannot be used on non-wasm /// targets or on wasm when the `core` feature is enabled, as neither of those compilation states are valid /// for the compilation of a vessel. pub use derive::export; pub use derive::kind; #[doc(hidden)] pub use futures; #[doc(hidden)] pub use lazy_static; #[doc(hidden)] pub use serde; #[doc(hidden)] pub use void; /// A type with a bijection to over-the-wire data. /// /// Kind is an advanced distributed object or RPC system that permits the over-the-wire serialization /// and deserialization of an implicitly flattened version of the complex nested structures required /// to produce a full type-level isomorphic representation of arbitrary composed data types. /// /// Vessels provides `Kind` implementations for many primitive types from the standard library as /// well as futures, streams, a variety of boxed function types, and more. /// /// Vessels also provides a derive macro that automatically generates `Kind` implementations for /// structs and enums in addition to the `object` macro for generating `Kind` implementations for /// trait objects of user-defined traits. /// /// Authors of third-party crates are encouraged to derive or implement Kind or Kind providers for /// types their crates expose that might be useful over some form of wire boundary, be it network, IPC, /// or any other similar transport. pub trait Kind: Any + Sized + Sync + Send + Unpin + 'static { /// The item transmitted over the network **to** the construction task /// from deconstruction. type ConstructItem: Serialize + DeserializeOwned + Send + Sync + Unpin + 'static; /// The failure condition of constructing a concrete type from communicated data. type ConstructError: ErrorBound; /// The concrete future type returned by the construction process. type ConstructFuture: Future<Output = ConstructResult<Self>> + Sync + Send + 'static; /// Constructs the `Kind` from the provided channel. This method should return /// immediately and, if necessary, move `channel` into some shim structure, /// async block, or other owned state specified by `ConstructFuture`. fn construct<C: Channel<Self::ConstructItem, Self::DeconstructItem>>( channel: C, ) -> Self::ConstructFuture; /// The item transmitted over the network **from** the construction task /// to deconstruction. type DeconstructItem: Serialize + DeserializeOwned + Send + Sync + Unpin + 'static; /// The failure condition of constructing a concrete type from communicated data. type DeconstructError: ErrorBound; /// The concrete future type returned by the deconstruction process. This is /// used to only to communicate failure of deconstruction and does not return /// a value. type DeconstructFuture: Future<Output = DeconstructResult<Self>> + Sync + Send + 'static; /// Moves out of the `Kind` and deconstructs on to the provided channel. /// As with `construct`, this method should return immediately. fn deconstruct<C: Channel<Self::DeconstructItem, Self::ConstructItem>>( self, channel: C, ) -> Self::DeconstructFuture; #[doc(hidden)] const USE_KIND_MACRO_TO_GENERATE_THIS_FIELD: [u8; 32]; } /// An erased representation of any serializable type used in communication /// by `Kind`. pub(crate) trait SerdeAny: erased_serde::Serialize + Downcast + Sync + Send {} #[doc(hidden)] pub trait ErrorBound: Error + Sync + Send + 'static {} impl<T: Error + Sync + Send + 'static> ErrorBound for T {} impl_downcast!(SerdeAny); serialize_trait_object!(SerdeAny); impl<T: ?Sized> SerdeAny for T where T: ErasedSerialize + Downcast + Sync + Send {} /// Logs information to a target-appropriate console. /// /// `log!` uses the same syntax as `format!`, `println!`, etc. and delegates to `format!` under the hood. /// ``` /// use vessels::log; /// /// log!("the answer is {}", 12); /// ``` /// Currently not supported inside individual vessels. #[macro_export] macro_rules! log { ($($args:expr),*) => ( let formatted = format!($($args,)*); $crate::core::LOG.info(formatted) ); ($($args:expr,)*) => ( let formatted = format!($($args,)*); $crate::core::LOG.info(formatted) ); } #[cfg(all(feature = "core", target_arch = "wasm32"))] use { ::core::pin::Pin, futures::task::{Context, Poll}, }; #[cfg(all( feature = "core", target_arch = "wasm32", not(target_feature = "atomics") ))] unsafe impl<F: Future> Send for SyncSendAssert<F> {} #[cfg(all( feature = "core", target_arch = "wasm32", not(target_feature = "atomics") ))] unsafe impl<F: Future> Sync for SyncSendAssert<F> {} #[cfg(all(feature = "core", target_arch = "wasm32"))] impl<F: Future> Future for SyncSendAssert<F> { type Output = F::Output; fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { self.0.as_mut().poll(cx) } } #[cfg(all(feature = "core", target_arch = "wasm32"))] pub(crate) struct SyncSendAssert<F: Future>(Pin<Box<F>>);