Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite serialization to use generators #9145

Closed
erickt opened this issue Sep 12, 2013 · 1 comment
Closed

Rewrite serialization to use generators #9145

erickt opened this issue Sep 12, 2013 · 1 comment

Comments

@erickt
Copy link
Contributor

erickt commented Sep 12, 2013

This is a placeholder bug to track potentially converting the serialization api to use generators (#7746). Once we have generators, We'll be able to write a serializer like this:

struct A {
    a: int,
    b: B,
}

Struct B {
    c: ~str,
    d: ~[int],
}

impl serialize::Encodable for A {
    fn encode_iter<'a>(&'a self) -> Option<Value<'a>> {
        yield StructStart("A");
        yield StructField("a", 0);
        yield Int(self.a);
        yield StructField("b", 1);
        for value in self.b.encode_iter() { yield value; }
        yield StructEnd;
    }
}

impl serialize::Encodable for B {
    fn encode_iter<'a>(&'a self) -> Option<Value<'a>> {
        yield StructStart("B");
        yield StructField("c", 0);
        yield String(self.c);
        yield StructField("d", 1);
        for value in self.d.encode_iter() { yield value; }
        yield StructEnd;
    }
}

A Deserializer would also be pretty simple:

struct PrettyPrinter<T> {
    it: T
}

impl<T: Iterator<Value>> serialize::Decoder<()> for PrettyPrinter {
    fn decode(&mut self) -> () {
        match self.it.next() {
            None => fail!(),
            Some(Int(x)) => printfln!("%?", x),
            Some(Str(x)) => printfln!("%?", x),
            Some(StructStart(name)) => {
                printfln!("%s {", name);
                self.decode_struct()
            }
            ...
        }
    }
    fn decode_struct(&mut self) {
        match self.it.next() {
            Some(StructField(name, idx)) => {
                if idx != 0 { printfln!(","); }
                printfln!("%s:", name);
                self.decode();
            }
            Some(StructEnd) => printfln!("}"),
            _ => fail!(),
        }
    }
    ...
} 

There are two advantages to this conversion. First, it's easier for rustc to optimize iterator-based code instead of recursive-closure-based code. Second, it allows for a serializer like extra::json::Encoder peek into the iterator stream to validate that a map key is actually a string. This would fix #8883.

@thestinger
Copy link
Contributor

Generators aren't going to happen for a long time (if ever - it needs to make it through the RFC process), so I don't think it makes sense to have bugs tracking use cases. It would be better to collect this kind of information on a wiki or a etherpad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants