An implementation of MCP-49.
```rust
fn decodeescapestring() { let escapedtext = "Hello,\x20world!\n"; let text: String = escapedtext .chars() .filter_map(co!(|c| { loop { if c != '\' { // Not escaped yield Some(c); continue; }
// Go past the \
yield None;
// Unescaped-char
match c {
// Hexadecimal
'x' => {
yield None; // Go past the x
let most = c.to_digit(16);
yield None; // Go past the first digit
let least = c.to_digit(16);
// Yield the decoded char if valid
yield (|| char::from_u32(most? << 4 | least?))()
}
// Simple escapes
'n' => yield Some('\n'),
'r' => yield Some('\r'),
't' => yield Some('\t'),
'0' => yield Some('\0'),
'\\' => yield Some('\\'),
// Unnecessary escape
_ => yield Some(c),
}
}
}))
.collect();
assert_eq!(text, "Hello, world!\n");
} ```
For the details of the proposal, see https://lang-team.rust-lang.org/designnotes/generalcoroutines.html.
Differences between this implementation and the proposal are summarized below:
FnPin
is provided. Yield closures made with this crate use Box::pin
internally and hence FnMut
.return
expressions.!
type. Thus it is compatible with both of the two designs of yield closures discussed in the document of MCP-49: poisoning by default or not.