Struct bincode::RefBox
[−]
[src]
pub struct RefBox<'a, T: 'a> { /* fields omitted */ }
A struct for encoding nested reference types.
Encoding large objects by reference is really handy. For example,
encode(&large_hashmap, ...)
encodes the large structure without having to
own the hashmap. However, it is impossible to serialize a reference if that
reference is inside of a struct.
// Not possible, rustc can not decode the reference. #[derive(RustcEncoding, RustcDecoding)] struct Message<'a> { big_map: &'a HashMap<u32, u32>, message_type: String, }
This is because on the decoding side, you can't create the Message struct because it needs to have a reference to a HashMap, which is impossible because during deserialization, all members need to be owned by the deserialized object.
This is where RefBox comes in. During serialization, it serializs a reference, but during deserialization, it puts that sub-object into a box!
// This works! #[derive(RustcEncoding, RustcDecoding)] struct Message<'a> { big_map: RefBox<'a, HashMap<u32, u32>>, message_type: String }
Now we can write
let my_map = HashMap::new(); let my_msg = Message { big_map: RefBox::new(&my_map), message_type: "foo".to_string() }; let encoded = encode(&my_msg, ...).unwrap(); let decoded: Message<'static> = decode(&encoded[]).unwrap();
Notice that we managed to encode and decode a struct with a nested reference
and that the decoded message has the lifetime 'static
which shows us
that the message owns everything inside it completely.
Please don't stick RefBox inside deep data structures. It is much better suited in the outermost layer of whatever it is that you are encoding.
Methods
impl<'a, T> RefBox<'a, T>
[src]
impl<T> RefBox<'static, T>
[src]
pub fn take(self) -> Box<T>
[src]
Takes the value out of this refbox.
Fails if this refbox was not created out of a deserialization.
Unless you are doing some really weird things with static references, this function will never fail.
pub fn try_take(self) -> Result<Box<T>, RefBox<'static, T>>
[src]
Tries to take the value out of this refbox.
Trait Implementations
impl<'a, T: Debug + 'a> Debug for RefBox<'a, T>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<'a, T: PartialEq + 'a> PartialEq for RefBox<'a, T>
[src]
fn eq(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests for !=
.
impl<'a, T: PartialOrd + 'a> PartialOrd for RefBox<'a, T>
[src]
fn partial_cmp(&self, __arg_0: &RefBox<'a, T>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &RefBox<'a, T>) -> bool
[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<'a, T: Eq + 'a> Eq for RefBox<'a, T>
[src]
impl<'a, T: Ord + 'a> Ord for RefBox<'a, T>
[src]
fn cmp(&self, __arg_0: &RefBox<'a, T>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
impl<'a, T: Hash + 'a> Hash for RefBox<'a, T>
[src]
fn hash<__HT: Hasher>(&self, __arg_0: &mut __HT)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<'a, T: Clone + 'a> Clone for RefBox<'a, T>
[src]
fn clone(&self) -> RefBox<'a, T>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<'a, T: Encodable> Encodable for RefBox<'a, T>
[src]
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>
[src]
Serialize a value using an Encoder
.
impl<T: Decodable> Decodable for RefBox<'static, T>
[src]
fn decode<D: Decoder>(d: &mut D) -> Result<RefBox<'static, T>, D::Error>
[src]
Deserialize a value using a Decoder
.