pub trait ForeignType {
// Required methods
fn assignable_from(&self, type_: &Type) -> bool;
fn assignable_to(&self, type_: &Type) -> bool;
fn type_ref(&self) -> TokenStream;
fn decoding_gen(
&self,
source: TokenStream,
output_ref: TokenStream,
arguments: Vec<TokenStream>,
is_async: bool,
) -> TokenStream;
fn encoding_gen(
&self,
target: TokenStream,
field_ref: TokenStream,
arguments: Vec<TokenStream>,
is_async: bool,
) -> TokenStream;
fn arguments(&self) -> Vec<TypeArgument>;
fn can_receive_auto(&self) -> Option<ScalarType>;
// Provided methods
fn assignable_from_partial(&self, type_: &PartialType) -> bool { ... }
fn assignable_to_partial(&self, type_: &PartialType) -> bool { ... }
}
Expand description
An encodable and decodable foreign type object Generally these are used to represent complexly encoded types, where there isn’t a notion of smaller internal types
Good examples: Custom encoded integers (i.e. varints) UTF8 strings
Bad examples: GZIP encoded data Encrypted data
Required Methods§
Sourcefn assignable_from(&self, type_: &Type) -> bool
fn assignable_from(&self, type_: &Type) -> bool
Is this type assignable from this other type? i.e. let X: ThisType = OtherType;
Sourcefn assignable_to(&self, type_: &Type) -> bool
fn assignable_to(&self, type_: &Type) -> bool
Is this type assignable to this other type?
i.e. let X: OtherType = ThisType;
Generally identical to ForeignType::assignable_from
Sourcefn type_ref(&self) -> TokenStream
fn type_ref(&self) -> TokenStream
Emits this type as a Rust type
Sourcefn decoding_gen(
&self,
source: TokenStream,
output_ref: TokenStream,
arguments: Vec<TokenStream>,
is_async: bool,
) -> TokenStream
fn decoding_gen( &self, source: TokenStream, output_ref: TokenStream, arguments: Vec<TokenStream>, is_async: bool, ) -> TokenStream
output code should be a term expression that:
- the expression should read its input from an implicit identifier
reader
as a&mut R
where R: Read - can read an arbitrary number of bytes from
reader
- returns a value of the foreign type
Sourcefn encoding_gen(
&self,
target: TokenStream,
field_ref: TokenStream,
arguments: Vec<TokenStream>,
is_async: bool,
) -> TokenStream
fn encoding_gen( &self, target: TokenStream, field_ref: TokenStream, arguments: Vec<TokenStream>, is_async: bool, ) -> TokenStream
output code should be a single statement that:
- takes an expression
field_ref
as a reference to a value of the foreign type - the statement should write its output to an implicit identifier
writer
as a&mut W
where W: Write
Sourcefn arguments(&self) -> Vec<TypeArgument>
fn arguments(&self) -> Vec<TypeArgument>
All arguments that can be passed to this type to describe characteristics (i.e. string length) All optional arguments must come at the end of the list of arguments.
Sourcefn can_receive_auto(&self) -> Option<ScalarType>
fn can_receive_auto(&self) -> Option<ScalarType>
If Some
, this type can receive auto
defined lengths during encoding
Provided Methods§
Sourcefn assignable_from_partial(&self, type_: &PartialType) -> bool
fn assignable_from_partial(&self, type_: &PartialType) -> bool
An internal modified form of ForeignType::assignable_from
to provide some flexibility in type checking.
Sourcefn assignable_to_partial(&self, type_: &PartialType) -> bool
fn assignable_to_partial(&self, type_: &PartialType) -> bool
An internal modified form of ForeignType::assignable_to
to provide some flexibility in type checking.