Skip to content

Instantly share code, notes, and snippets.

@erickt
Created September 18, 2012 02:03
Show Gist options
  • Save erickt/3740847 to your computer and use it in GitHub Desktop.
Save erickt/3740847 to your computer and use it in GitHub Desktop.
trait Serializer {
fn emit_uint(v: uint);
fn emit_rec(f: fn());
fn emit_rec_field(f_name: &str, f_idx: uint, f: fn());
}
trait Serializable {
fn serialize<S: Serializer>(s: S);
}
impl uint: Serializable {
fn serialize<S: Serializer>(s: S) { s.emit_uint(self) }
}
type t = { mut x: uint };
impl t: Serializable {
fn serialize<S: Serializer>(s: S) {
do s.emit_rec || {
s.emit_rec_field("x", 0, || self.x.serialize(s));
}
}
}
fn main() { }
foo3.rs:20:40: 20:46 error: illegal borrow unless pure: creating immutable alias to mutable field
foo3.rs:20 s.emit_rec_field("x", 0, || self.x.serialize(s));
^~~~~~
foo3.rs:20:40: 20:56 note: impure due to access to impure function
foo3.rs:20 s.emit_rec_field("x", 0, || self.x.serialize(s));
^~~~~~~~~~~~~~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment