How to set lifetime for boxed closure capturing `self`? - iterator

I want to return an iterator (in particular, a Filter). I used a previous answer that suggests returning a boxed iterator. The problem is that my filter captures self and I get:
error: closure may outlive the current function, but it borrows self, which is owned by the current function
But I think I'm being explicit that self has lifetime 'a and so does the Box I'm returning:
fn occupied_iter_x<'a>(&'a self) -> Box<Iterator<Item=i32> + 'a> {
Box::new( (0..32).filter( |&pos| match self.at(pos) { Occupied::Empty => false, _ => true } ) )
}
This is an attempt to simplify my first effort, where I created a new FooIterator which owns a reference to self in basically the same way.
If I use most of the same syntax to simply capture self as a member reference it works fine:
struct Foo { junk: i32 }
struct FooIterator<'a> { foo: &'a Foo }
impl Foo {
fn foo<'a>(&'a self) -> Box<FooIterator<'a>> {
Box::new( FooIterator { foo: self } )
}
}

You get an error because your closure receives a reference to self, which itself is a reference. However, since the reference points to a local variable, that reference becomes invalid as soon as the function returns.
To fix this, add the move keyword before the closure. This instructs the compiler to move the closed-over variables in the closure, rather than pass a reference to those variables to the closure.
fn occupied_iter_x<'a>(&'a self) -> Box<Iterator<Item=i32> + 'a> {
Box::new( (0..32).filter(move |&pos| match self.at(pos) { Occupied::Empty => false, _ => true } ) )
}

Related

What's the idiomatic way of creating a Rust Error shim?

I have two error types that are defined in external crates. Let's call them foo::Error and bar::Error. Because they are both defined externally, I cannot create a direct From or Into conversion between the two.
In my own crate, I'm using implementing a trait from bar, which expects return values of Result<T, bar::Error>. In the methods of my implementation, I'm calling functions from foo, which return many Result<T, foo::Error> types.
I would like to map these foo::Error errors to bar::Error errors, so that my trait implementation is neat, but the best that I can do is create a separate shim error, and then have a clunky .map_err(ShimErr::from)? on everything. This can start to litter up my code if I'm calling many foo:: functions in my implementation.
use foo;
use bar;
struct ShimErr(foo::Error);
impl From<foo::Error> for ShimErr {
fn from(e: foo::Error) -> Self {
Self(e)
}
}
impl From<ShimErr> for bar::Error {
fn from(e: ShimErr) -> Self {
Self{}
}
}
struct MyTraitImpl {}
impl bar::SomeTrait for MyTraitImpl {
fn do_something() -> Result<i32, bar::Error> {
// FIXME: THIS IS CLUNKY
let foo_val: i32 = foo::call_something().map_err(ShimErr::from)?;
Ok(foo_val * 2)
}
}
Is there a better way? In an ideal world, there would be a way to just use ?
If I remove map_err and try to use ? directly:
error[E0277]: `?` couldn't convert the error to `bar::Error`
--> shimtest.rs:32:49
|
32 | let foo_val: i32 = foo::call_something()?;
| ^ the trait `std::convert::From<foo::Error>` is not implemented for `bar::Error`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following implementations were found:
<bar::Error as std::convert::From<ShimErr>>
= note: required by `std::convert::From::from`
The way I handle multiple different errors from other crates would be to make my own Error enum and use that instead of other errors directly.
So instead of a tuple struct that only contains foo::Error, you can create an enum that can have both foo::Error and bar::Error. After this, you can convert all the errors to the custom enum error with ?.
In certain cases, I think you'll still need to use something like .map_err(Into::into) occasionally, but usually only when the variable being returned is a Result with an external error.
use bar::Error as BarError;
use foo::Error as FooError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::result::Result as StdResult;
/// Common result type that uses the custom error
pub type Result<T> = StdResult<T, Error>;
/// Common error type used as a holder for errors from various other libraries.
#[derive(Debug)]
pub enum Error {
Bar(BarError),
Foo(FooError),
}
impl From<BarError> for Error {
fn from(err: BarError) -> Error {
Error::Bar(err)
}
}
impl From<FooError> for Error {
fn from(err: FooError) -> Error {
Error::Foo(err)
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
Error::Bar(ref inner) => inner.fmt(f),
Error::Foo(ref inner) => inner.fmt(f),
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Bar(ref inner) => inner.description(),
Error::Foo(ref inner) => inner.description(),
}
}
}
Now you can use the custom Result and Error as follows.
// this Result is now our custom Result type with custom error
// same as std::result::Result<i32, Error> (with Error as our custom error)
fn do_something() -> Result<i32> {
let foo_val: i32 = foo::call_something()?; // should convert to our custom error
Ok(foo_val * 2)
}
fn do_something_else() -> Result<i32> {
// directly returning a Result<> with a different error type will need map_err
foo::call_something().map_err(Into::into)
}

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 ;-)

About synchronized scope in Swift

Objective-C:
#synchronized {
return;
}
NSLog(#"This line of code does not run.");
Objective-C's #synchronized is a language-level directive and does not introduce a new function scope.
Swift:
synchronized {
return
}
print("This line of code does run.")
func synchronized(lock: AnyObject, #noescape closure: () -> Void) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
Synchronized uses closures which do introduce a function scope.
Question:
Can I do the same in Swift? Does not introduce a new function scope.
Real Problem
public func stopRecording() {
synchronized(self) {
guard status == .Recording else { return }
status = .StoppingRecording
}
// Many somethings need to do, and I don`t want to write into the closure.
finishRecording()
......
......
......
}
Workaround:
if (synchronized(self) { return true }) {
return
}
print(#"This line of code does not run.")
func synchronized(lock: AnyObject, #noescape closure: () -> Bool) -> Bool {
objc_sync_enter(lock)
defer {
objc_sync_exit(lock)
}
return closure()
}
Someone has a elegant solution?
I just put together a solution that isn't too bad. We cannot introduce a custom language construct that doesn't introduce a new scope, therefore we need to use one of the existing ones. The if statement should work for that. I made a small class for that, which does but one thing: Lock the given object and check that the unlock method got called before it deallocates (along with some additions for debugging):
final class LockingManager {
let obj: AnyObject
let file: StaticString
let line: UInt
var locked = true
init(obj: AnyObject, file: StaticString, line: UInt) {
self.obj = obj
self.file = file
self.line = line
objc_sync_enter(obj)
}
func unlock() {
objc_sync_exit(obj)
locked = false
}
deinit {
precondition(!locked, "Object \(obj) not unlocked", file: file, line: line)
}
}
With a function like this:
func lock(obj: AnyObject, file: StaticString = #file, line: UInt = #line) -> (() -> Void)? {
return LockingManager(obj: obj, file: file, line: line).unlock
}
We can use it like this:
if let unlock = lock(obj: self) {
defer { unlock() }
// Do stuff
}
Since method never returns nil, you don't ever want an else part. Since the unlock method, which holds the only reference to the LockingManager, is only available within the if statement, after the statement the LockingManager gets deallocated. Upon deallocation, it checks whether the unlock method was called and if not, throws an error.
I understand that this isn't a super nice solution, but it does work in that it doesn't introduce a new scope (and additionally checks for correct usage).
Another solution would be to just use simple function calls and a do block:
do {
objc_sync_enter(self)
defer { objc_sync_exit(self) }
// Do stuff
}
I personally prefer the second approach (with some better named functions), because it's much clearer what it does.

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.

Objects and classes in Rust

I am fiddling around with Rust, going by the examples, trying to make a class. I have been looking at the example of StatusLineText
It keeps raising the errors:
error: `self` is not available in a static method. Maybe a `self` argument is missing? [E0424]
self.id + self.extra
^~~~
error: no method named `get_total` found for type `main::Thing` in the current scope
println!("the thing's total is {}", my_thing.get_total());
^~~~~~~~~
My code is rather simple:
fn main() {
struct Thing {
id: i8,
extra: i8,
}
impl Thing {
pub fn new() -> Thing {
Thing { id: 3, extra: 2 }
}
pub fn get_total() -> i8 {
self.id + self.extra
}
}
let my_thing = Thing::new();
println!("the thing's total is {}", my_thing.get_total());
}
You need to add an explicit self parameter to make methods:
fn get_total(&self) -> i8 {
self.id + self.extra
}
Functions without the explicit self parameter are considered associated functions, which can be called without a specific instance.