Skip to content

Commit

Permalink
Update PR with comments
Browse files Browse the repository at this point in the history
- Change to different signatures for simplicity
  • Loading branch information
the10thWiz committed Aug 14, 2023
1 parent 55aeb82 commit 3ffa761
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions core/lib/src/fairing/fairings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl Fairings {
Fairings::default()
}

pub(crate) fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Box<dyn Fairing>> + 'a {
self.all_fairings.iter_mut()
pub(crate) fn iter_mut(&mut self) -> impl Iterator<Item = &mut dyn Fairing> + '_ {
self.all_fairings.iter_mut().map(|f| f.as_mut())
}

pub fn active(&self) -> impl Iterator<Item = &usize> {
Expand Down
1 change: 1 addition & 0 deletions core/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")]
#![cfg_attr(nightly, feature(doc_cfg))]
#![cfg_attr(nightly, feature(decl_macro))]
#![cfg_attr(nightly, feature(trait_upcasting))]

#![warn(rust_2018_idioms)]
#![warn(missing_docs)]
Expand Down
49 changes: 24 additions & 25 deletions core/lib/src/rocket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::any::TypeId;
use std::any::{TypeId, Any};
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::net::SocketAddr;
Expand Down Expand Up @@ -519,34 +519,33 @@ impl Rocket<Build> {
/// If multiple instances of the same type of Fairing have been attached, the modify function
/// will be run on each instance.
#[must_use]
pub fn modify_fairings<F: Fairing, E: fmt::Debug>(mut self, modifier: impl Fn(&mut F) -> Result<(), E>) -> Result<Self, Self> {
#[derive(Debug, PartialEq, Eq)]
enum State {
None,
Success,
Failure,
pub fn modify_fairings<F: Fairing, E: fmt::Debug>(mut self, modifier: impl Fn(&mut F)) -> Self {
// Note: the nightly version of this function uses trait upcasting, and
// should be prefered once [trait upcasting](https://github.com/rust-lang/rust/issues/65991)
// is stable. The nightly version is primarily for testing, and enabled the compiler to
// verify this is safe on nightly.
#[cfg(feature = "nightly")]
fn downcast_mut<T: Fairing>(f: &mut dyn Fairing) -> Option<&mut T> {
(f as &mut Any + Send + Sync).downcast_mut()
}
let mut state = State::None;
for f in self.fairings.iter_mut() {
let f = f.as_mut();
if f.type_id() == TypeId::of::<F>() {
// SAFTEY: `f` is a valid pointer since it was obtained from a Box, and it points
// at an `F` since it has the same TypeId. Converting from dyn type to a concrete
// type is handled automatically by the compilier
let f = unsafe { &mut *((f as *mut dyn Fairing) as *mut F) };
match modifier(f) {
Ok(()) => if state == State::None { state = State::Success },
Err(e) => {
error_!("Failed to modify fairing: {:?}", e);
state = State::Failure;
}
}
// The implementation of downcast_mut is taken from `std::any::Any::downcast_mut`
#[cfg(not(feature = "nightly"))]
fn downcast_mut<T: Fairing>(fairing: &mut dyn Fairing) -> Option<&mut T> {
// Note: Any::type_id is needed here to avoid an incorrect guess as to whether we want
// the type_id of `&mut dyn Fairing` (we don't) or the inner type.
if Any::type_id(&*fairing) == TypeId::of::<T>() {
// SAFTEY: `f` is a valid pointer since it was a valid reference, and it points
// at an `F` since it has the same underlying TypeId. Converting from dyn type to
// a concrete type is handled automatically by the compilier.
Some(unsafe { &mut *((fairing as *mut dyn Fairing) as *mut T) })
} else {
None
}
}
match state {
State::None | State::Failure => Err(self),
State::Success => Ok(self),
for f in self.fairings.iter_mut().filter_map(|f| downcast_mut(f)) {
modifier(f);
}
self
}

/// Returns a `Future` that transitions this instance of `Rocket` into the
Expand Down

0 comments on commit 3ffa761

Please sign in to comment.