Rust idiomatic way of polymorphic struct
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.
To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.
use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;
trait Handler
fn get_type(&self) -> String;
struct FileHandler
struct DbHandler
impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()
impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()
struct Config
handlers: Vec<Box<Handler>>
impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());
write!(f, "config")
pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();
let fh = FileHandler ;
let dh = DbHandler ;
handlers.push(Box::new(fh));
handlers.push(Box::new(dh));
let c = Config handlers ;
println!(":?", c);
Is there a way of more cleaner or more idiomatic solution here?
rust polymorphism
 |Â
show 3 more comments
up vote
1
down vote
favorite
I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.
To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.
use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;
trait Handler
fn get_type(&self) -> String;
struct FileHandler
struct DbHandler
impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()
impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()
struct Config
handlers: Vec<Box<Handler>>
impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());
write!(f, "config")
pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();
let fh = FileHandler ;
let dh = DbHandler ;
handlers.push(Box::new(fh));
handlers.push(Box::new(dh));
let c = Config handlers ;
println!(":?", c);
Is there a way of more cleaner or more idiomatic solution here?
rust polymorphism
1
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
1
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
1
Just a little question: why isConfig
generic?
â Boiethios
Apr 18 at 7:40
1
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace theself.handlers.iter()
in yourfor
loop with&self.handlers
(as&Vec<T>
implementsIntoIterator<&T>
) , and you can drop the braces from structs with no fields.
â Joe Clay
Apr 18 at 15:38
1
You probably meant to usewriteln!(f, ...)?;
in yourDebug
impl instead ofprintln!(...);
.
â Francis Gagné
Apr 19 at 2:08
 |Â
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.
To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.
use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;
trait Handler
fn get_type(&self) -> String;
struct FileHandler
struct DbHandler
impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()
impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()
struct Config
handlers: Vec<Box<Handler>>
impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());
write!(f, "config")
pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();
let fh = FileHandler ;
let dh = DbHandler ;
handlers.push(Box::new(fh));
handlers.push(Box::new(dh));
let c = Config handlers ;
println!(":?", c);
Is there a way of more cleaner or more idiomatic solution here?
rust polymorphism
I've implemented a simple polymorphic structure, which looks a bit ugly and I'm afraid I am missing an idiomatic way of doing it.
To describe briefly what I'm trying to achieve here: I would like to have a structure with a collection of handlers, which all must implement specific trait.
use std::fmt;
use std::fmt::Formatter;
use std::fmt::Error;
trait Handler
fn get_type(&self) -> String;
struct FileHandler
struct DbHandler
impl Handler for FileHandler
fn get_type(&self) -> String
"file".to_string()
impl Handler for DbHandler
fn get_type(&self) -> String
"db".to_string()
struct Config
handlers: Vec<Box<Handler>>
impl fmt::Debug for Config
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
for h in self.handlers.iter()
println!("", h.get_type());
write!(f, "config")
pub fn run()
let mut handlers: Vec<Box<Handler>> = Vec::new();
let fh = FileHandler ;
let dh = DbHandler ;
handlers.push(Box::new(fh));
handlers.push(Box::new(dh));
let c = Config handlers ;
println!(":?", c);
Is there a way of more cleaner or more idiomatic solution here?
rust polymorphism
edited Apr 18 at 8:06
asked Apr 17 at 17:02
Stormherz
1062
1062
1
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
1
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
1
Just a little question: why isConfig
generic?
â Boiethios
Apr 18 at 7:40
1
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace theself.handlers.iter()
in yourfor
loop with&self.handlers
(as&Vec<T>
implementsIntoIterator<&T>
) , and you can drop the braces from structs with no fields.
â Joe Clay
Apr 18 at 15:38
1
You probably meant to usewriteln!(f, ...)?;
in yourDebug
impl instead ofprintln!(...);
.
â Francis Gagné
Apr 19 at 2:08
 |Â
show 3 more comments
1
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
1
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
1
Just a little question: why isConfig
generic?
â Boiethios
Apr 18 at 7:40
1
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace theself.handlers.iter()
in yourfor
loop with&self.handlers
(as&Vec<T>
implementsIntoIterator<&T>
) , and you can drop the braces from structs with no fields.
â Joe Clay
Apr 18 at 15:38
1
You probably meant to usewriteln!(f, ...)?;
in yourDebug
impl instead ofprintln!(...);
.
â Francis Gagné
Apr 19 at 2:08
1
1
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
1
1
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
1
1
Just a little question: why is
Config
generic?â Boiethios
Apr 18 at 7:40
Just a little question: why is
Config
generic?â Boiethios
Apr 18 at 7:40
1
1
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the
self.handlers.iter()
in your for
loop with &self.handlers
(as &Vec<T>
implements IntoIterator<&T>
) , and you can drop the braces from structs with no fields.â Joe Clay
Apr 18 at 15:38
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the
self.handlers.iter()
in your for
loop with &self.handlers
(as &Vec<T>
implements IntoIterator<&T>
) , and you can drop the braces from structs with no fields.â Joe Clay
Apr 18 at 15:38
1
1
You probably meant to use
writeln!(f, ...)?;
in your Debug
impl instead of println!(...);
.â Francis Gagné
Apr 19 at 2:08
You probably meant to use
writeln!(f, ...)?;
in your Debug
impl instead of println!(...);
.â Francis Gagné
Apr 19 at 2:08
 |Â
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192308%2frust-idiomatic-way-of-polymorphic-struct%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Can you expand on what you mean by "looks a bit ugly"? Ugliness is not a universally shared concept.
â Shepmaster
Apr 17 at 17:04
1
@Shepmaster It's ugly for me as a newcomer to rust, practically, not in a way that rust is ugly, but how I've done it =) But I can't really say how it should look in a better implementation. I guess by "ugly" I mean I'm not quite sure it's idiomatic enough.
â Stormherz
Apr 17 at 17:09
1
Just a little question: why is
Config
generic?â Boiethios
Apr 18 at 7:40
1
Not a full answer to the question, but a couple of stylistic tweaks you might like - you can replace the
self.handlers.iter()
in yourfor
loop with&self.handlers
(as&Vec<T>
implementsIntoIterator<&T>
) , and you can drop the braces from structs with no fields.â Joe Clay
Apr 18 at 15:38
1
You probably meant to use
writeln!(f, ...)?;
in yourDebug
impl instead ofprintln!(...);
.â Francis Gagné
Apr 19 at 2:08