Skip to content

Commit e632c44

Browse files
committed
auto merge of #10658 : LeoTestard/rust/serialize-rc, r=cmr
Implement various traits (IterBytes and extra's Encodable and Decodable) for Rc<T> when T alreay implements the trait.
2 parents 6fe6a6f + ae836c1 commit e632c44

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/libextra/serialize.rs

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Core encoding and decoding interfaces.
2020

2121
use std::at_vec;
2222
use std::hashmap::{HashMap, HashSet};
23+
use std::rc::Rc;
2324
use std::trie::{TrieMap, TrieSet};
2425
use std::vec;
2526
use ringbuf::RingBuf;
@@ -405,6 +406,20 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T {
405406
}
406407
}
407408

409+
impl<S:Encoder,T:Encodable<S> + Freeze> Encodable<S> for Rc<T> {
410+
#[inline]
411+
fn encode(&self, s: &mut S) {
412+
self.borrow().encode(s)
413+
}
414+
}
415+
416+
impl<D:Decoder,T:Decodable<D> + Freeze> Decodable<D> for Rc<T> {
417+
#[inline]
418+
fn decode(d: &mut D) -> Rc<T> {
419+
Rc::new(Decodable::decode(d))
420+
}
421+
}
422+
408423
impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @T {
409424
fn decode(d: &mut D) -> @T {
410425
@Decodable::decode(d)

src/libstd/to_bytes.rs

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use cast;
1818
use container::Container;
1919
use iter::Iterator;
2020
use option::{None, Option, Some};
21+
use rc::Rc;
2122
use str::{Str, StrSlice};
2223
use vec::{Vector, ImmutableVector};
2324

@@ -325,6 +326,13 @@ impl<A:IterBytes> IterBytes for @mut A {
325326
}
326327
}
327328

329+
impl<A:IterBytes> IterBytes for Rc<A> {
330+
#[inline]
331+
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
332+
self.borrow().iter_bytes(lsb0, f)
333+
}
334+
}
335+
328336
impl<A:IterBytes> IterBytes for ~A {
329337
#[inline]
330338
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {

0 commit comments

Comments
 (0)