using dyn explicitly, per rust 1.37 deprecation warning

This commit is contained in:
Dustin Carlino 2019-08-22 14:19:35 -07:00
parent 9f897ef094
commit f8aaf4d7bf
19 changed files with 64 additions and 58 deletions

View File

@ -7,25 +7,25 @@ use std::any::Any;
pub trait Cloneable: CloneableImpl {}
pub trait CloneableImpl {
fn clone_box(&self) -> Box<Cloneable>;
fn as_any(&self) -> &Any;
fn clone_box(&self) -> Box<dyn Cloneable>;
fn as_any(&self) -> &dyn Any;
}
impl<T> CloneableImpl for T
where
T: 'static + Cloneable + Clone,
{
fn clone_box(&self) -> Box<Cloneable> {
fn clone_box(&self) -> Box<dyn Cloneable> {
Box::new(self.clone())
}
fn as_any(&self) -> &Any {
fn as_any(&self) -> &dyn Any {
self
}
}
impl Clone for Box<Cloneable> {
fn clone(&self) -> Box<Cloneable> {
impl Clone for Box<dyn Cloneable> {
fn clone(&self) -> Box<dyn Cloneable> {
self.clone_box()
}
}
@ -34,5 +34,5 @@ impl Cloneable for () {}
impl Cloneable for usize {}
impl Cloneable for f64 {}
impl Cloneable for String {}
impl Cloneable for (String, Box<Cloneable>) {}
impl Cloneable for (String, Box<dyn Cloneable>) {}
impl Cloneable for WeightedUsizeChoice {}

View File

@ -221,7 +221,7 @@ impl FileWithProgress {
// Also hands back a callback that'll add the final result to the timer. The caller must run
// it.
// TODO It's really a FnOnce, but I don't understand the compiler error.
pub fn new(path: &str) -> Result<(FileWithProgress, Box<Fn(&mut Timer)>), Error> {
pub fn new(path: &str) -> Result<(FileWithProgress, Box<dyn Fn(&mut Timer)>), Error> {
let file = File::open(path)?;
let path_copy = path.to_string();
let total_bytes = file.metadata()?.len() as usize;

View File

@ -31,7 +31,10 @@ impl Progress {
}
// Returns when done
fn next<'a>(&mut self, maybe_sink: &mut Option<Box<TimerSink + 'a>>) -> Option<(f64, String)> {
fn next<'a>(
&mut self,
maybe_sink: &mut Option<Box<dyn TimerSink + 'a>>,
) -> Option<(f64, String)> {
self.processed_items += 1;
if self.processed_items > self.total_items {
panic!(
@ -106,7 +109,7 @@ pub struct Timer<'a> {
notes: Vec<String>,
pub(crate) warnings: Vec<String>,
sink: Option<Box<TimerSink + 'a>>,
sink: Option<Box<dyn TimerSink + 'a>>,
}
struct TimerSpan {
@ -130,7 +133,7 @@ impl<'a> Timer<'a> {
t
}
pub fn new_with_sink(name: &str, sink: Box<TimerSink + 'a>) -> Timer<'a> {
pub fn new_with_sink(name: &str, sink: Box<dyn TimerSink + 'a>) -> Timer<'a> {
let mut t = Timer::new(name);
t.sink = Some(sink);
t
@ -146,7 +149,7 @@ impl<'a> Timer<'a> {
}
// Workaround for borrow checker
fn selfless_println(maybe_sink: &mut Option<Box<TimerSink + 'a>>, line: String) {
fn selfless_println(maybe_sink: &mut Option<Box<dyn TimerSink + 'a>>, line: String) {
println!("{}", line);
if let Some(ref mut sink) = maybe_sink {
sink.println(line);

View File

@ -11,7 +11,7 @@ use std::path::PathBuf;
pub struct PickABTest;
impl PickABTest {
pub fn new() -> Box<State> {
pub fn new() -> Box<dyn State> {
WizardState::new(Box::new(pick_ab_test))
}
}
@ -93,7 +93,7 @@ impl State for ABTestSetup {
}
}
fn make_load_savestate(ab_test: ABTest) -> Box<State> {
fn make_load_savestate(ab_test: ABTest) -> Box<dyn State> {
WizardState::new(Box::new(move |wiz, ctx, ui| {
let ss = wiz.wrap(ctx).choose_string("Load which savestate?", || {
abstutil::list_dir(std::path::Path::new(&abstutil::path1(

View File

@ -17,7 +17,7 @@ impl Cloneable for Shortcut {}
pub struct ChoosingShortcut;
impl ChoosingShortcut {
pub fn new() -> Box<State> {
pub fn new() -> Box<dyn State> {
WizardState::new(Box::new(choose_shortcut))
}
}

View File

@ -11,7 +11,7 @@ const WARP_TO_CAM_ZOOM: f64 = 10.0;
pub struct EnteringWarp;
impl EnteringWarp {
pub fn new() -> Box<State> {
pub fn new() -> Box<dyn State> {
WizardState::new(Box::new(warp_to))
}
}

View File

@ -11,7 +11,7 @@ pub struct BusRouteExplorer {
}
impl BusRouteExplorer {
pub fn new(ctx: &mut EventCtx, ui: &UI) -> Option<Box<State>> {
pub fn new(ctx: &mut EventCtx, ui: &UI) -> Option<Box<dyn State>> {
let map = &ui.primary.map;
let routes = match ui.primary.current_selection {
Some(ID::BusStop(bs)) => map.get_routes_serving_stop(bs),
@ -84,7 +84,7 @@ impl State for BusRouteExplorer {
pub struct BusRoutePicker;
impl BusRoutePicker {
pub fn new(ui: &UI, menu: &mut ModalMenu) -> Option<Box<State>> {
pub fn new(ui: &UI, menu: &mut ModalMenu) -> Option<Box<dyn State>> {
if !menu.action("explore a bus route") {
return None;
}
@ -99,7 +99,7 @@ impl BusRoutePicker {
}
}
fn make_bus_route_picker(choices: Vec<BusRouteID>) -> Box<State> {
fn make_bus_route_picker(choices: Vec<BusRouteID>) -> Box<dyn State> {
WizardState::new(Box::new(move |wiz, ctx, ui| {
let (_, id) = wiz
.wrap(ctx)

View File

@ -10,7 +10,7 @@ const TILE_DIMS: f64 = 2.0;
pub struct ColorChooser;
impl ColorChooser {
pub fn new() -> Box<State> {
pub fn new() -> Box<dyn State> {
WizardState::new(Box::new(pick_color))
}
}

View File

@ -14,7 +14,7 @@ pub struct Floodfiller {
}
impl Floodfiller {
pub fn new(ctx: &mut EventCtx, ui: &UI, parent_menu: &mut ModalMenu) -> Option<Box<State>> {
pub fn new(ctx: &mut EventCtx, ui: &UI, parent_menu: &mut ModalMenu) -> Option<Box<dyn State>> {
let map = &ui.primary.map;
let (reachable_lanes, mut prompt) = if let Some(ID::Lane(l)) = ui.primary.current_selection
{

View File

@ -475,7 +475,7 @@ pub fn apply_map_edits(
bundle.map.simplify_edits(&mut timer);
}
fn make_bulk_edit_lanes(road: RoadID) -> Box<State> {
fn make_bulk_edit_lanes(road: RoadID) -> Box<dyn State> {
WizardState::new(Box::new(move |wiz, ctx, ui| {
let mut wizard = wiz.wrap(ctx);
let (_, from) = wizard.choose_something("Change all lanes of type...", || {

View File

@ -273,7 +273,7 @@ fn change_traffic_signal(
apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits);
}
fn make_change_cycle_duration(current_duration: Duration) -> Box<State> {
fn make_change_cycle_duration(current_duration: Duration) -> Box<dyn State> {
WizardState::new(Box::new(move |wiz, ctx, _| {
let new_duration = wiz.wrap(ctx).input_usize_prefilled(
"How long should this cycle be?",
@ -290,7 +290,7 @@ fn make_change_cycle_duration(current_duration: Duration) -> Box<State> {
}))
}
fn make_change_preset(i: IntersectionID) -> Box<State> {
fn make_change_preset(i: IntersectionID) -> Box<dyn State> {
WizardState::new(Box::new(move |wiz, ctx, ui| {
let (_, new_signal) = wiz
.wrap(ctx)

View File

@ -8,7 +8,7 @@ use ezgui::{Canvas, EventCtx, EventLoopMode, GfxCtx, Wizard, GUI};
// top-level game states.
pub struct Game {
// A stack of states
pub states: Vec<Box<State>>,
pub states: Vec<Box<dyn State>>,
pub ui: UI,
idx_draw_base: Option<usize>,
@ -19,7 +19,7 @@ impl Game {
let splash = !flags.no_splash
&& !format!("{}", flags.sim_flags.load.display()).contains("data/save");
let ui = UI::new(flags, ctx, splash);
let states: Vec<Box<State>> = if splash {
let states: Vec<Box<dyn State>> = if splash {
vec![Box::new(SplashScreen::new_with_screensaver(ctx, &ui))]
} else {
vec![
@ -185,27 +185,27 @@ pub enum Transition {
Keep,
Pop,
// If a state needs to pass data back to the parent, use this. Sadly, runtime type casting.
PopWithData(Box<FnOnce(&mut Box<State>, &mut UI, &mut EventCtx)>),
Push(Box<State>),
Replace(Box<State>),
PopWithData(Box<dyn FnOnce(&mut Box<dyn State>, &mut UI, &mut EventCtx)>),
Push(Box<dyn State>),
Replace(Box<dyn State>),
// These don't.
KeepWithMode(EventLoopMode),
PopWithMode(EventLoopMode),
PushWithMode(Box<State>, EventLoopMode),
ReplaceWithMode(Box<State>, EventLoopMode),
PushWithMode(Box<dyn State>, EventLoopMode),
ReplaceWithMode(Box<dyn State>, EventLoopMode),
}
pub struct WizardState {
wizard: Wizard,
// Returning None means stay in this WizardState
cb: Box<Fn(&mut Wizard, &mut EventCtx, &mut UI) -> Option<Transition>>,
cb: Box<dyn Fn(&mut Wizard, &mut EventCtx, &mut UI) -> Option<Transition>>,
}
impl WizardState {
pub fn new(
cb: Box<Fn(&mut Wizard, &mut EventCtx, &mut UI) -> Option<Transition>>,
) -> Box<State> {
cb: Box<dyn Fn(&mut Wizard, &mut EventCtx, &mut UI) -> Option<Transition>>,
) -> Box<dyn State> {
Box::new(WizardState {
wizard: Wizard::new(),
cb,

View File

@ -329,7 +329,7 @@ impl DrawMap {
pub struct AgentCache {
time: Option<Duration>,
agents_per_on: HashMap<Traversable, Vec<Box<Renderable>>>,
agents_per_on: HashMap<Traversable, Vec<Box<dyn Renderable>>>,
// cam_zoom also matters
unzoomed: Option<(f64, Drawable)>,
}
@ -350,7 +350,7 @@ impl AgentCache {
.collect()
}
pub fn put(&mut self, now: Duration, on: Traversable, agents: Vec<Box<Renderable>>) {
pub fn put(&mut self, now: Duration, on: Traversable, agents: Vec<Box<dyn Renderable>>) {
if Some(now) != self.time {
self.agents_per_on.clear();
self.time = Some(now);

View File

@ -68,7 +68,7 @@ pub fn draw_vehicle(
prerender: &Prerender,
cs: &ColorScheme,
acs: AgentColorScheme,
) -> Box<Renderable> {
) -> Box<dyn Renderable> {
if input.id.1 == VehicleType::Bike {
Box::new(DrawBike::new(input, map, prerender, cs, acs))
} else {

View File

@ -38,7 +38,7 @@ impl AgentSpawner {
ctx: &mut EventCtx,
ui: &mut UI,
sandbox_menu: &mut ModalMenu,
) -> Option<Box<State>> {
) -> Option<Box<dyn State>> {
let menu = ModalMenu::new(
"Agent Spawner",
vec![vec![(hotkey(Key::Escape), "quit")]],

View File

@ -76,8 +76,8 @@ impl UI {
&self,
g: &mut GfxCtx,
opts: DrawOptions,
source: &GetDrawAgents,
show_objs: &ShowObject,
source: &dyn GetDrawAgents,
show_objs: &dyn ShowObject,
) {
let ctx = DrawCtx {
cs: &self.cs,
@ -193,8 +193,8 @@ impl UI {
pub fn calculate_current_selection(
&self,
ctx: &EventCtx,
source: &GetDrawAgents,
show_objs: &ShowObject,
source: &dyn GetDrawAgents,
show_objs: &dyn ShowObject,
debug_mode: bool,
) -> Option<ID> {
// Unzoomed mode. Ignore when debugging areas and extra shapes.
@ -246,8 +246,8 @@ impl UI {
bounds: Bounds,
prerender: &Prerender,
agents: &'a mut AgentCache,
source: &GetDrawAgents,
show_objs: &ShowObject,
source: &dyn GetDrawAgents,
show_objs: &dyn ShowObject,
) -> Vec<&'a (dyn Renderable + 'a)> {
let map = &self.primary.map;
let draw_map = &self.primary.draw_map;
@ -315,7 +315,7 @@ impl UI {
for on in &agents_on {
if !agents.has(time, *on) {
let mut list: Vec<Box<Renderable>> = Vec::new();
let mut list: Vec<Box<dyn Renderable>> = Vec::new();
for c in source.get_draw_cars(*on, map).into_iter() {
list.push(draw_vehicle(c, map, prerender, &self.cs, self.agent_cs));
}

View File

@ -10,12 +10,12 @@ use std::collections::VecDeque;
pub struct Wizard {
alive: bool,
tb: Option<TextBox>,
menu: Option<Menu<Box<Cloneable>>>,
menu: Option<Menu<Box<dyn Cloneable>>>,
log_scroller: Option<LogScroller>,
slider: Option<SliderWithTextBox>,
// In the order of queries made
confirmed_state: Vec<Box<Cloneable>>,
confirmed_state: Vec<Box<dyn Cloneable>>,
}
impl Wizard {
@ -82,7 +82,7 @@ impl Wizard {
query: &str,
prefilled: Option<String>,
input: &mut UserInput,
parser: Box<Fn(String) -> Option<R>>,
parser: Box<dyn Fn(String) -> Option<R>>,
) -> Option<R> {
assert!(self.alive);
@ -152,7 +152,7 @@ pub struct WrappedWizard<'a, 'b> {
ctx: &'a mut EventCtx<'b>,
// The downcasts are safe iff the queries made to the wizard are deterministic.
ready_results: VecDeque<Box<Cloneable>>,
ready_results: VecDeque<Box<dyn Cloneable>>,
}
impl<'a, 'b> WrappedWizard<'a, 'b> {
@ -160,7 +160,7 @@ impl<'a, 'b> WrappedWizard<'a, 'b> {
&mut self,
query: &str,
prefilled: Option<String>,
parser: Box<Fn(String) -> Option<R>>,
parser: Box<dyn Fn(String) -> Option<R>>,
) -> Option<R> {
if !self.ready_results.is_empty() {
let first = self.ready_results.pop_front().unwrap();
@ -245,9 +245,9 @@ impl<'a, 'b> WrappedWizard<'a, 'b> {
if !self.ready_results.is_empty() {
let first = self.ready_results.pop_front().unwrap();
// We have to downcast twice! \o/
let pair: &(String, Box<Cloneable>) = first
let pair: &(String, Box<dyn Cloneable>) = first
.as_any()
.downcast_ref::<(String, Box<Cloneable>)>()
.downcast_ref::<(String, Box<dyn Cloneable>)>()
.unwrap();
let item: &R = pair.1.as_any().downcast_ref::<R>().unwrap();
return Some((pair.0.to_string(), item.clone()));
@ -278,7 +278,7 @@ impl<'a, 'b> WrappedWizard<'a, 'b> {
));
return None;
}
let boxed_choices: Vec<(Option<MultiKey>, String, Box<Cloneable>)> = choices
let boxed_choices: Vec<(Option<MultiKey>, String, Box<dyn Cloneable>)> = choices
.into_iter()
.map(|(multikey, s, item)| (multikey, s, item.clone_box()))
.collect();

View File

@ -487,13 +487,13 @@ impl PolyLine {
),
])
} else {
return Warn::warn(
Warn::warn(
vec![self.make_polygons(arrow_thickness)],
format!(
"Can't make_arrow_outline of outline_thickness {} for {}",
outline_thickness, self
),
);
)
}
}
@ -550,7 +550,7 @@ impl PolyLine {
if pts.len() == 1 {
return None;
}
return Some(PolyLine::new(pts));
Some(PolyLine::new(pts))
} else {
panic!("Can't get_slice_ending_at: {} doesn't contain {}", self, pt);
}
@ -566,7 +566,7 @@ impl PolyLine {
let mut pts = self.pts.clone();
pts = pts.split_off(idx + 1);
pts.insert(0, pt);
return Some(PolyLine::new(pts));
Some(PolyLine::new(pts))
} else {
panic!(
"Can't get_slice_starting_at: {} doesn't contain {}",

View File

@ -40,7 +40,10 @@ impl PopDat {
trips: Vec::new(),
parcels: BTreeMap::new(),
};
let fields: Vec<(&str, Box<Fn(&mut TractData, BTreeMap<String, Estimate>)>)> = vec![
let fields: Vec<(
&str,
Box<dyn Fn(&mut TractData, BTreeMap<String, Estimate>)>,
)> = vec![
(
"../data/input/household_vehicles.kml",
Box::new(|tract, map| {