Grundsätzlich möchte ich eine Funktion schreiben, die eine Schließung zurückgibt. Wie kann ich das tun, ohne Box<FnOnce(u32)>
zurückgeben zu müssen?Wie erstellt man eine Größe Schließung oder implementieren Sie Fn/FnMut/FnOnce auf einer Struktur?
Aus der closures chapter of the rust book, ich lese, dass ein Abschluss ist nur syntaktischer Zucker für eine Struktur und ein Impl von FnOnce
. Hier ist mein Versuch:
#[derive(Debug)]
struct MyError {
code: u32,
location: &'static str,
}
// Here is my closure:
struct MyErrorPartial {
location: &'static str,
}
impl FnOnce(u32) for MyErrorPartial {
type Output = MyError;
fn call_once(self, args: u32) -> MyError {
MyError {
code: args,
location: self.location,
}
}
}
fn error_at(location: &'static str) -> MyErrorPartial {
MyErrorPartial {location: location}
}
fn function_returning_code() -> Result<(), u32> {
Err(123)
}
fn function_with_error() -> Result<(), MyError> {
try!(function_returning_code().map_err(error_at("line1")));
try!(function_returning_code().map_err(error_at("line2")));
Ok(())
}
fn main() {
function_with_error().unwrap();
}
Aktuelle Zeit einen Fehler gibt:
<anon>:11:12: 11:17 error: associated type bindings are not allowed here [E0229]
<anon>:11 impl FnOnce(u32) for MyErrorPartial {
^~~~~
Ist es unmöglich, 'Fn *' in stabiler zu implementieren? – Kapichu
@Kapichu Es scheint so: Die Dokumente für die Methode ['call_once'] (https://doc.rust-lang.org/std/ops/trait.FnOnce.html#typmethod.call_once) verweisen auf [# 29625 ] (https://github.com/rust-lang/rust/issues/29625), die noch offen ist. –