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

Remove uses of FnBox #851

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions core/lib/src/fairing/ad_hoc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Mutex;
use std::boxed::FnBox;

use {Rocket, Request, Response, Data};
use fairing::{Fairing, Kind, Info};
Expand Down Expand Up @@ -43,14 +42,14 @@ pub struct AdHoc {

enum AdHocKind {
/// An ad-hoc **attach** fairing. Called when the fairing is attached.
Attach(Mutex<Option<Box<FnBox(Rocket) -> Result<Rocket, Rocket> + Send + 'static>>>),
Attach(Mutex<Option<Box<dyn FnOnce(Rocket) -> Result<Rocket, Rocket> + Send + 'static>>>),
/// An ad-hoc **launch** fairing. Called just before Rocket launches.
Launch(Mutex<Option<Box<FnBox(&Rocket) + Send + 'static>>>),
Launch(Mutex<Option<Box<dyn FnOnce(&Rocket) + Send + 'static>>>),
/// An ad-hoc **request** fairing. Called when a request is received.
Request(Box<Fn(&mut Request, &Data) + Send + Sync + 'static>),
Request(Box<dyn Fn(&mut Request, &Data) + Send + Sync + 'static>),
/// An ad-hoc **response** fairing. Called when a response is ready to be
/// sent to a client.
Response(Box<Fn(&Request, &mut Response) + Send + Sync + 'static>),
Response(Box<dyn Fn(&Request, &mut Response) + Send + Sync + 'static>),
}

impl AdHoc {
Expand Down Expand Up @@ -146,9 +145,10 @@ impl Fairing for AdHoc {
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
if let AdHocKind::Attach(ref mutex) = self.kind {
let mut option = mutex.lock().expect("AdHoc::Attach lock");
option.take()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra let binding should be unnecessary -- just replace the line .call_box((rocket,)) with (rocket).

Copy link
Contributor Author

@KamilaBorowska KamilaBorowska Dec 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary, however it looks ugly when having a function call in a multi-line chain like this.

.expect("internal error: `on_attach` single-call invariant broken")
.call_box((rocket,))
let f = option
.take()
.expect("internal error: `on_attach` single-call invariant broken");
f(rocket)
} else {
Ok(rocket)
}
Expand All @@ -157,9 +157,10 @@ impl Fairing for AdHoc {
fn on_launch(&self, rocket: &Rocket) {
if let AdHocKind::Launch(ref mutex) = self.kind {
let mut option = mutex.lock().expect("AdHoc::Launch lock");
option.take()
.expect("internal error: `on_launch` single-call invariant broken")
.call_box((rocket, ))
let f = option
.take()
.expect("internal error: `on_launch` single-call invariant broken");
f(rocket)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![feature(specialization)]
#![feature(decl_macro)]
#![feature(try_trait)]
#![feature(fnbox)]
#![feature(never_type)]
#![feature(proc_macro_hygiene)]
#![feature(crate_visibility_modifier)]
#![feature(try_from)]
#![feature(label_break_value)]
#![feature(unsized_locals)]

#![recursion_limit="256"]

Expand Down