mirror of https://github.com/fafhrd91/actix-web
add support for Guard for Rc<dyn Guard> made Files clonable again
This commit is contained in:
parent
7416630d65
commit
7e3c30f155
|
@ -37,7 +37,7 @@ pub struct Files {
|
|||
mime_override: Option<Rc<MimeOverride>>,
|
||||
file_flags: named::Flags,
|
||||
use_guards: Option<Rc<dyn Guard>>,
|
||||
guards: Vec<Box<dyn Guard>>,
|
||||
guards: Vec<Box<Rc<dyn Guard>>>,
|
||||
hidden_files: bool,
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,24 @@ impl fmt::Debug for Files {
|
|||
}
|
||||
}
|
||||
|
||||
impl Clone for Files {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
directory: self.directory.clone(),
|
||||
index: self.index.clone(),
|
||||
show_index: self.show_index,
|
||||
redirect_to_slash: self.redirect_to_slash,
|
||||
default: self.default.clone(),
|
||||
renderer: self.renderer.clone(),
|
||||
file_flags: self.file_flags,
|
||||
path: self.path.clone(),
|
||||
mime_override: self.mime_override.clone(),
|
||||
use_guards: self.use_guards.clone(),
|
||||
guards: self.guards.clone(),
|
||||
hidden_files: self.hidden_files,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Files {
|
||||
/// Create new `Files` instance for a specified base directory.
|
||||
///
|
||||
|
@ -189,7 +207,7 @@ impl Files {
|
|||
/// ```
|
||||
#[inline]
|
||||
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
||||
self.guards.push(Box::new(guard));
|
||||
self.guards.push(Box::new(Rc::new(guard)));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -234,7 +252,13 @@ impl HttpServiceFactory for Files {
|
|||
let guards = if self.guards.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(std::mem::take(&mut self.guards))
|
||||
let guards = std::mem::take(&mut self.guards);
|
||||
Some(
|
||||
guards
|
||||
.into_iter()
|
||||
.map(|guard| -> Box<dyn Guard> { guard })
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
};
|
||||
|
||||
if self.default.borrow().is_none() {
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
//! ```
|
||||
#![allow(non_snake_case)]
|
||||
use std::convert::TryFrom;
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_http::http::{self, header, uri::Uri};
|
||||
use actix_http::RequestHead;
|
||||
|
@ -40,6 +42,12 @@ pub trait Guard {
|
|||
fn check(&self, request: &RequestHead) -> bool;
|
||||
}
|
||||
|
||||
impl Guard for Rc<dyn Guard> {
|
||||
fn check(&self, request: &RequestHead) -> bool {
|
||||
self.deref().check(request)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create guard object for supplied function.
|
||||
///
|
||||
/// ```rust
|
||||
|
|
Loading…
Reference in New Issue