Changing a enum's field in Rust - oop

I am very new to Rust but I am trying to work out how to modify an instance of an enum. I need to use managed boxes for other reasons, but it seems to make changing an enum's field hard.
#[feature(managed_boxes)];
enum State { A(int), B }
fn main() {
let mut state = #A(123);
match *state {
A(ref mut i) => { *i = 456 }
B => { }
}
println!("{:?}", state)
}
I get the error cannot borrow immutable anonymous field as mutable. The mut seems to only say that state, the variable, is mutable. I want to tell Rust the whole thing is mutable. I find forced immutability one of the most annoying things of Rust.

Some time ago managed boxed had their own "hierarchy" of mutability. The following code used to work then:
#[feature(managed_boxes)];
enum State { A(int), B }
fn main() {
let state = #mut A(123);
match *state {
A(ref mut i) => { *i = 456 }
B => { }
}
println!("{:?}", state)
}
But managed boxes are scheduled to be removed from the language. In current version of Rust #mut is not a valid token. You have to use RefCell, a mutable cell which provides mutability inside managed pointers. Currently it looks somewhat like this:
#[feature(managed_boxes)];
use std::cell::RefCell;
enum State { A(int), B }
fn main() {
let state = #RefCell::new(A(123));
{
let mut r = state.borrow_mut();
match r.get() {
&A(ref mut i) => { *i = 456 }
&B => { }
}
}
println!("{:?}", state)
}
You will get rather extensive output on the terminal though, because it will print internals of RefCell structure. See documentation on std::cell module for more information on cells and how to use them.
In the future Rust won't have special syntax for managed boxes at all. Garbage collection will be implemented in libraries. I believe the code will look like this (Rust authors, please correct me if I'm wrong):
use std::cell::RefCell;
enum State { A(int), B }
fn main() {
// state is of type Gc<RefCell<State>>
let state = box(Gc) RefCell::new(A(123));
// Dereference will work for library pointer types, not sure about autodereference
let mut r = (*state).borrow_mut();
match r.get() {
&A(ref mut i) => { *i = 456 }
&B => { }
}
println!("{:?}", *state)
}

Related

How do I return a Result containing every error from an iterator of Results, not just the first one?

I'm trying to implement a simple interpreter in Rust, for which I have created a Tokens struct, which takes source characters and produces either a Token or a ScanError inside a Result:
pub struct Tokens<'src> {
chars: Chars<'src>,
}
impl<'src> Iterator for Tokens<'src> {
type Item = Result<Token, ScanError>;
fn next(&mut self) -> Option<Result<Token, ScanError>> {
// ...
}
}
Since Result implements FromIterator, it is simple to collect the result to either the first ScanError or a vector of Tokens:
fn scan_tokens(source: &str) -> Result<Vec<Token>, ScanError> {
let iter = Tokens {
chars: source.chars(),
};
iter.collect()
}
In the case of multiple errors I really want to return every error:
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
// what goes here?
}
It isn't possible as far as I know to implement my own version of FromIterator because neither that trait or Result are local to my crate. Can anyone suggest a clean way of doing this?
I have written an implementation using partition on the iterator, then unwrapping each Result, below, but it's not fun to read and doesn't feel like good use of iterators:
type T = Vec<Result<Token, ScanError>>;
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
let iter = Tokens {
chars: source.chars(),
};
let (tokens_results, error_results): (T, T) = iter.partition(|result| result.is_ok());
let errors: Vec<ScanError> = error_results
.into_iter()
.map(|result| result.unwrap_err())
.collect();
if errors.len() > 0 {
return Err(errors);
}
Ok(tokens_results
.into_iter()
.map(|result| result.unwrap())
.collect())
}
unwrapping each Result
I would use itertools' partition_map to avoid the need to unwrap:
use itertools::{Either, Itertools}; // 0.8.0
fn iterator() -> impl Iterator<Item = Result<i32, bool>> {
vec![Ok(1), Err(false), Ok(2), Err(true), Ok(3)].into_iter()
}
fn example() -> Result<Vec<i32>, Vec<bool>> {
let (values, errors): (Vec<_>, Vec<_>) = iterator().partition_map(|v| match v {
Ok(v) => Either::Left(v),
Err(e) => Either::Right(e),
});
if errors.is_empty() {
Ok(values)
} else {
Err(errors)
}
}
See also:
What's the most idiomatic way of working with an Iterator of Results?
How do I stop iteration and return an error when Iterator::map returns a Result::Err?
How do I perform iterator computations over iterators of Results without collecting to a temporary vector?
You could also use the fact that Option and Result implement IntoIterator to avoid the exact unwrap, although this still processes one collection twice:
fn example2() -> Result<Vec<i32>, Vec<bool>> {
let (values, errors): (Vec<_>, Vec<_>) = iterator().partition(|result| result.is_ok());
if errors.is_empty() {
Ok(values.into_iter().flat_map(Result::ok).collect())
} else {
Err(errors.into_iter().flat_map(Result::err).collect())
}
}
See also:
Why does `Option` support `IntoIterator`?
An imperative solution is often the most expressive and efficient way to implement some algorithm. It's Rust, not Haskell; not everything needs to be functional.
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
let iter = Tokens {
chars: source.chars(),
};
let mut tokens = Vec::new();
let mut errors = Vec::new();
for result in iter {
match result {
Ok(token) => {
tokens.push(token);
}
Err(e) => {
errors.push(e);
}
}
}
if errors.is_empty() {
Ok(tokens)
} else {
Err(errors)
}
}

Wrong number of lifetime parameters when using a modified `Chars` iterator

I want to implement the IntoIterator trait for a struct containing a String. The iterator is based on the chars() iterator, is supposed to count the '1' chars and accumulate the result. This is a simplified version of what I got so far:
use std::iter::Map;
use std::str::Chars;
fn main() {
let str_struct = StringStruct { system_string: String::from("1101") };
for a in str_struct {
println!("{}", a);
}
}
struct StringStruct {
system_string: String
}
impl IntoIterator for StringStruct {
type Item = u32;
type IntoIter = Map<Chars, Fn(char) -> u32>;
fn into_iter(self) -> Self::IntoIter {
let count = 0;
return self.system_string.chars().map(|c| match c {
Some('1') => {
count += 1;
return Some(count);
},
Some(chr) => return Some(count),
None => return None
});
}
}
Expected output: 1, 2, 2, 3
This fails with:
error[E0107]: wrong number of lifetime parameters: expected 1, found 0
--> src/main.rs:17:25
|
17 | type IntoIter = Map<Chars, Fn(char) -> u32>;
| ^^^^^ expected 1 lifetime parameter
The chars iterator should have the same lifetime as the StringStruct::system_string, but I have no idea how to express this or if this approach is viable at all.
To answer the question you asked, I'd recommend to impl IntoIterator for &StringStruct (a reference to a StringStruct instead of the struct directly). The code would look like this:
impl<'a> IntoIterator for &'a StringStruct {
type Item = u32;
type IntoIter = Map<Chars<'a>, Fn(char) -> u32>;
// ...
}
However, you will notice many more errors that have a different origin afterwards. The next error that pops up is that Fn(char) -> u32 does not have a constant size at compile time.
The problem is that you try to name the type of your closure by writing Fn(char) -> u32. But this is not the type of your closure, but merely a trait which is implemented by the closure. The type of a closure can't be named (sometimes called "Voldemort type").
This means that, right now, you can't specify the type of a Map<_, _> object. This is a known issue; the recently accepted impl Trait-RFC might offer a workaround for cases like this. But right now, it's not possible, sorry.
So how to solve it then? You need to create your own type that implements Iterator and use it instead of Map<_, _>. Note that you can still use the Chars iterator. Here is the full solution:
struct StringStructIter<'a> {
chars: Chars<'a>,
count: u32,
}
impl<'a> Iterator for StringStructIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.chars.next().map(|c| {
if c == '1' {
self.count += 1;
}
self.count
})
}
}
impl<'a> IntoIterator for &'a StringStruct {
type Item = u32;
type IntoIter = StringStructIter<'a>;
fn into_iter(self) -> Self::IntoIter {
StringStructIter {
chars: self.system_string.chars(),
count: 0,
}
}
}
fn main() {
let str_struct = StringStruct { system_string: String::from("1101") };
for a in &str_struct {
println!("{}", a);
}
}
And just a small note: an explicit return when not necessary is considered bad style in Rust. Better stick to rule and write idiomatic code by removing return whenever possible ;-)

What does the ampersand (&) before `self` mean in Rust?

I've seen this code in the Rust documentation:
fn eat(&self) {
println!("{} is done eating.", self.name);
}
what does the & in &self mean?
This means you'll be passing in a reference to the object, as opposed to moving the object itself. It's important to distinguish this because if your function looked like:
fn eat(self) {
println!("{} is done eating.", self.name);
}
and you tried calling it then using the variable after, you'd get an error
object = Foo::new();
object.eat();
object.something(); // error, because you moved object in eat
because when you don't specify &, rust moves the value into the function and your original binding no longer has ownership. check out this minimal example I created (playground version):
struct Foo {
x : u32
}
impl Foo {
fn eat(self) {
println!("eating");
}
fn something(&self) {
println!("else");
}
}
fn main() {
println!("Hello, world!");
let g = Foo { x: 5 };
g.eat();
g.something(); // if this comes before eat, no errors because we arent moving
}
Now switch something to be called before eat. Because something only takes a reference, g still has ownership and you can continue on. eat on the other hand moves g and you no longer can use g.

Return an arbitrary Encodable from a match expression?

I have a match expression than can return several (builtin and custom) types, which will ultimately be serialized to JSON and returned from a web request. I would prefer to avoid repeating the serialization code or making a string copy in each match arm.
Each arm returns an Encodable; however, it seems that Encodable is not object-safe, so I cannot make a pointer to it.
Edit: Due to changes in Rust, the question has gone from "Is this a good way to do this?" to "How can I do this at all?" (This is with version rustc 1.0.0-nightly (ed530d7a3 2015-01-16 22:41:16 +0000))
extern crate "rustc-serialize" as rustc_serialize;
use rustc_serialize::{json, Encodable};
#[derive(RustcEncodable)]
struct Valid {
value: u32
}
#[derive(RustcEncodable)]
struct Error {
error: &'static str // '
}
fn main() {
let valid = true;
let result = match valid {
true => Box::new(Valid { value: 42 }) as Box<Encodable>,
false => Box::new(Error { error: "bork" }) as Box<Encodable>
};
let mut buf = String::new();
result.encode(&mut json::Encoder::new(&mut buf)).unwrap();
println!("{}", buf);
}
error: cannot convert to a trait object because trait `rustc-serialize::serialize::Encodable` is not object-safe [E0038]
There are 2 ways in which traits can be used in Rust:
As bounds in generic functions (static dispatch)
As trait objects, behind pointers (dynamic dispatch)
Because Encodable is not object-safe, we can't use dynamic dispatch, because the compiler doesn't allow us to create a pointer to an Encodable.
Therefore, we have to use static dispatch. To do this, I've moved the code that works on the Encodable to a new, generic function, and called it from each arm.
extern crate "rustc-serialize" as rustc_serialize;
use rustc_serialize::{json, Encodable};
#[derive(RustcEncodable)]
struct Valid {
value: u32
}
#[derive(RustcEncodable)]
struct Error {
error: &'static str // '
}
fn do_encode<E: Encodable>(e: E) -> () {
let mut buf = String::new();
e.encode(&mut json::Encoder::new(&mut buf)).unwrap();
println!("{}", buf);
}
fn main() {
let is_valid = true;
match is_valid {
true => do_encode(Valid { value: 42 }),
false => do_encode(Error { error: "bork" }),
};
}

Rust: Create an Iterator out of Default and Succ?

I have the following code in a repo:
impl<Id> IdAllocator<Id> where
Id : Clone + Default + Add<u32, Id>,
{
pub fn new() -> IdAllocator<Id> {
IdAllocator {
next: Default::default()
}
}
// Produce an Id that hasn't been produced yet by this object.
pub fn allocate(&mut self) -> Id {
let ret = self.next.clone();
self.next = self.next + 1;
ret
}
}
But it seems a little clumsy, especially since the Add instance is only used as a succ function (generating the next value in sequence). Is there some Succ class I can use? And if so, is there already some Iterator construction somewhere in the standard library that already does this Default+Succ pattern?
Thanks!
No, unfortunately, there is no Succ-like thing in the standard library. The closest thing you can find is range() family of iterators, however, it uses Add and One numeric traits to generate items. You can do it this way (the idea is basically the same as yours, but this version is slightly more generic due to One trait usage):
use std::num::One;
use std::default::Default;
struct IdAllocator<T> {
current: T
}
impl<T: Default> IdAllocator<T> {
#[inline]
pub fn new() -> IdAllocator<T> {
IdAllocator {
current: Default::default()
}
}
}
impl<T: Add<T, T>+One+Clone> Iterator<T> for IdAllocator<T> {
fn next(&mut self) -> Option<T> {
let next = self.current + One::one();
self.current = next.clone();
Some(next)
}
}
fn main() {
let a = IdAllocator::<uint>::new();
for i in a.take(10) {
println!("{}", i);
}
}
(try it here)