Let S be a struct. I want to implement From for all uint types. Is there a terse way to do this?
E.g., I want to write this code
impl From<S> for usize {
fn from(s: S) -> usize {
s.field_a + s.field_b
}
}
impl From<S> for u64 {
fn from(s: S) -> u64 {
s.field_a + s.field_b
}
}
impl From<S> for u32 {
fn from(s: S) -> u32 {
s.field_a + s.field_b
}
}
...
as
impl From<S> for uint {
fn from(s: S) -> uint {
s.field_a + s.field_b
}
}
My first thought was to use traits to group all the uints a la how we pass traits as parameters. Here's my attempt:
use std::ops::Add;
impl From<S> for impl From + Add {
fn from<T: From + Add>(s: S) -> T {
T::from(s.field_a) + T::from(s.field_b)
}
}
But this doesn't work and feels janky (uints aren't just things that impl From and Add).
Don't know where to go from here! Any help would be appreciated!
A macro could work. (playground)
struct S {
field_a: u8,
field_b: u8,
}
macro_rules! impl_from_s {
($($uint_type: ty),*) => {
$(
impl From<S> for $uint_type {
fn from(s: S) -> $uint_type {
<$uint_type>::from(s.field_a) + <$uint_type>::from(s.field_b)
}
}
)*
}
}
impl_from_s!(u8, u16, u32, u64, u128);
Related
I am new to Rust, and does not fully understand lifetime, so probably, that is why I can't solv the following issue. I need a solution in which a class has a heterogeneous HashMap containing different objects derived from the same trait.
I have to be able to extend an object with some (multiple) functionality dinamically. Other solutions are also welcome. Adding functionality to the class in compile time could also work, but adding functionality directly to the main class not.
use std::collections::HashMap;
trait DoerTrait {
fn do_something( & self, a : u8, b : u8 ) -> u8;
}
struct MyDoer<'a> {
}
impl DoerTrait for MyDoer<'a> {
fn do_something( & self, a : u8, b : u8 ) -> u8 {
return a + b;
}
}
struct MyMain<'a> {
doers : HashMap<u8,&'a dyn DoerTrait>,
}
impl<'a> MyMain<'a> {
fn new() -> Self {
Self {
doers : HashMap::new()
}
}
fn add_doer( &mut self, id : u8, doer : & dyn DoerTrait ) {
self.doers.insert( id, doer );
}
fn do_something( & self, id : u8 ) {
match self.doers.get( &id ) {
Some( doer ) => {
println!( "{}", doer(19,26) );
}
None => {
println!( "Doer not specified!" );
}
}
}
}
fn main() {
let mut mymain = MyMain::new();
let mydoer = MyDoer{};
mymain.add_doer( 42, &mydoer );
mymain.do_something( 42 );
}
Not too sure what issue you have, once MyDoer has been stripped of its incorrect (unnecessary) lifetime and the lifetime has correctly been declared on impl MyMain, the compiler directly points to the parameter of add_doer not matching (after which it points out that doer in do_something is not a function):
use std::collections::HashMap;
trait DoerTrait {
fn do_something(&self, a: u8, b: u8) -> u8;
}
struct MyDoer;
impl DoerTrait for MyDoer {
fn do_something(&self, a: u8, b: u8) -> u8 {
return a + b;
}
}
struct MyMain<'a> {
doers: HashMap<u8, &'a dyn DoerTrait>,
}
impl<'a> MyMain<'a> {
fn new() -> Self {
Self {
doers: HashMap::new(),
}
}
fn add_doer(&mut self, id: u8, doer: &'a dyn DoerTrait) {
self.doers.insert(id, doer);
}
fn do_something(&self, id: u8) {
match self.doers.get(&id) {
Some(doer) => {
println!("{}", doer.do_something(19, 26));
}
None => {
println!("Doer not specified!");
}
}
}
}
fn main() {
let mut mymain = MyMain::new();
let mydoer = MyDoer {};
mymain.add_doer(42, &mydoer);
mymain.do_something(42);
}
In c++, I can define a parent class, and the type in vector can just be the father class type. So how to implement that in Rust?
Like for this example:
I defined two types of Integer who both implement the trait Object, now I want to put them in a same vector, is there any way to achieve that?
pub trait Object<T: Object<T>+Clone> {
fn sub(&self, x: &T) -> T {
x.clone() //this is a useless implementation, just for structs don't need to implement this method.
}
}
#[derive(Debug, Copy, Clone)]
pub struct Integer {
val: i32
}
impl Integer {
pub fn get(&self) -> i32 {
self.val
}
pub fn new(val: i32) -> Self {
Integer {
val
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Int {
val: i32
}
impl Int {
pub fn get(&self) -> i32 {
self.val
}
pub fn new(val: i32) -> Self {
Int {
val
}
}
}
impl Object<Int> for Int {
fn sub(&self, rhs: &Int) -> Int {
Int {
val: self.val - rhs.get()
}
}
}
impl Object<Integer> for Integer {
fn sub(&self, rhs: &Integer) -> Integer {
Integer {
val: self.val - rhs.get()
}
}
}
fn main() {
let mut v: Vec<Box<dyn Object>> = Vec::new();
v.push(Box::new(Integer::new(1)));
v.push(Box::new(Int::new(2)));
}
Thanks a lot.
There are several aspects of your design that don't fit in Rust:
trait Object<T: Object<T>+Clone> doesn't help - Rust doesn't do CRTP, just use Self instead.
for Object to be object-safe (necessary to put it in a vector), it can't be parameterized by type. A type parameter means you get a completely separate trait for each type.
Object::sub() can't return the result by value, because the size of the value can differ for different implementations, so it wouldn't be object-safe. It must return Box<dyn Object> instead.
The code modified as indicated looks like this:
pub trait Object {
fn get(&self) -> i32;
fn sub(&self, x: &dyn Object) -> Box<dyn Object>;
}
#[derive(Debug, Copy, Clone)]
pub struct Integer {
val: i32,
}
impl Integer {
fn new(val: i32) -> Box<dyn Object> {
Box::new(Int { val })
}
}
impl Object for Integer {
fn get(&self) -> i32 {
self.val
}
fn sub(&self, rhs: &dyn Object) -> Box<dyn Object> {
Integer::new(self.val - rhs.get())
}
}
#[derive(Debug, Copy, Clone)]
pub struct Int {
val: i32,
}
impl Int {
fn new(val: i32) -> Box<dyn Object> {
Box::new(Int { val })
}
}
impl Object for Int {
fn get(&self) -> i32 {
self.val
}
fn sub(&self, rhs: &dyn Object) -> Box<dyn Object> {
Int::new(self.val - rhs.get())
}
}
fn main() {
let mut v: Vec<Box<dyn Object>> = vec![];
v.push(Integer::new(1));
v.push(Int::new(2));
v.push(v[0].sub(v[1].as_ref()));
for o in &v {
println!("{}", o.get());
}
}
Playground
I think you can combine trait and provide a blank implementation, then use that in vector
trait TraitA {}
trait TraitB {}
trait CombinedTraitATraitB: TraitA + TraitB {}
impl<T> CombinedTraitATraitB for T where T: TraitA + TraitB {}
let vector: Vec<Box<dyn CombinedTraitATraitB>> = vec![];
Playground link
I have several different structs grouped together in an enum:
pub enum Ty {
A(AStruct),
B(BStruct)
}
pub struct AStruct {
base: BaseStruct
}
impl AStruct {
base_struct_passthrough_impls!();
pub fn new(x: i32) -> Self {
Self {
base: BaseStruct::new(x)
}
}
}
pub struct BStruct {
base: BaseStruct
}
impl BStruct {
base_struct_passthrough_impls!();
pub fn new(x: i32) -> Self {
Self {
base: BaseStruct::new(x)
}
}
}
All of these types will share a base struct that is common to them all. This base struct will have a lot of methods that I don't want to duplicate for each supertype.
pub struct BaseStruct {
x: i32
}
impl BaseStruct {
pub fn new(x: i32) -> Self {
Self {
x
}
}
pub fn get_x(&self) -> i32 {
self.x
}
}
#[macro_export]
macro_rules! base_struct_passthrough_impls {
() => {
pub fn get_x(&self) -> i32 {
self.base.get_x()
};
}
}
However, trying to use this code results in the following error:
error: macro expansion ignores token `;` and any following
--> src/main.rs:37:10
|
37 | };
| ^
...
46 | base_struct_passthrough_impls!();
| --------------------------------- caused by the macro expansion here
|
= note: the usage of `base_struct_passthrough_impls!` is likely invalid in impl item context
It seems like macro_rules!() is not usable in the impl item context. Is this correct, and if so is there anyway around this restriction? Would a proc macro work here, or would doing something like this work better?
The issue isn't the macro usage, but the definition. You included a trailing semicolon after the function definition created by the macro, which is what causes the error. If you remove it, everything works fine - here's the code:
fn main() {
let types = vec![Ty::A(AStruct::new(32)), Ty::B(BStruct::new(64))];
types.iter().for_each(|item| {
dbg!(match item {
Ty::A(a_struct) => a_struct.get_x(),
Ty::B(b_struct) => b_struct.get_x(),
});
})
}
pub enum Ty {
A(AStruct),
B(BStruct)
}
pub struct BaseStruct {
x: i32
}
impl BaseStruct {
pub fn new(x: i32) -> Self {
Self {
x
}
}
pub fn get_x(&self) -> i32 {
self.x
}
}
#[macro_export]
macro_rules! base_struct_passthrough_impls {
() => {
pub fn get_x(&self) -> i32 {
self.base.get_x()
} // there was an illegal semicolon here
}
}
pub struct AStruct {
base: BaseStruct
}
impl AStruct {
base_struct_passthrough_impls!();
pub fn new(x: i32) -> Self {
Self {
base: BaseStruct::new(x)
}
}
}
pub struct BStruct {
base: BaseStruct
}
impl BStruct {
base_struct_passthrough_impls!();
pub fn new(x: i32) -> Self {
Self {
base: BaseStruct::new(x)
}
}
}
Some trait methods have default implementations which can be overwritten by an implementer. How can I use the default implementation for a struct that overwrites the default?
For example:
trait SomeTrait {
fn get_num(&self) -> i32;
fn add_to_num(&self) -> i32 {
self.get_num() + 1
}
}
struct SomeStruct;
impl SomeTrait for SomeStruct {
fn get_num(&self) -> i32 {
3
}
fn add_to_num(&self) -> i32 {
self.get_num() + 2
}
}
fn main() {
let the_struct = SomeStruct;
println!("{}", the_struct.add_to_num()); // how can I get this to print 4 instead of 5?
}
One solution I've come up with is to define a dummy struct that contains the struct I want to change. I can then cherry-pick which methods I want to overwrite and which ones I want to keep as the default.
To extend the original example:
trait SomeTrait {
fn get_num(&self) -> i32;
fn add_to_num(&self) -> i32 {
self.get_num() + 1
}
}
struct SomeStruct;
impl SomeTrait for SomeStruct {
fn get_num(&self) -> i32 {
3
}
fn add_to_num(&self) -> i32 {
self.get_num() + 2
}
}
fn main() {
struct SomeOtherStruct {
base: SomeStruct,
}
impl SomeTrait for SomeOtherStruct {
fn get_num(&self) -> i32 {
self.base.get_num()
}
//This dummy struct keeps the default behavior of add_to_num()
}
let the_struct = SomeStruct;
println!("{}", the_struct.add_to_num());
//now we can call the default method using the original struct's data.
println!("{}", SomeOtherStruct { base: the_struct }.add_to_num());
}
Some trait methods have default implementations which can be overwritten by an implementer. How can I use the default implementation for a struct that overwrites the default?
For example:
trait SomeTrait {
fn get_num(&self) -> i32;
fn add_to_num(&self) -> i32 {
self.get_num() + 1
}
}
struct SomeStruct;
impl SomeTrait for SomeStruct {
fn get_num(&self) -> i32 {
3
}
fn add_to_num(&self) -> i32 {
self.get_num() + 2
}
}
fn main() {
let the_struct = SomeStruct;
println!("{}", the_struct.add_to_num()); // how can I get this to print 4 instead of 5?
}
One solution I've come up with is to define a dummy struct that contains the struct I want to change. I can then cherry-pick which methods I want to overwrite and which ones I want to keep as the default.
To extend the original example:
trait SomeTrait {
fn get_num(&self) -> i32;
fn add_to_num(&self) -> i32 {
self.get_num() + 1
}
}
struct SomeStruct;
impl SomeTrait for SomeStruct {
fn get_num(&self) -> i32 {
3
}
fn add_to_num(&self) -> i32 {
self.get_num() + 2
}
}
fn main() {
struct SomeOtherStruct {
base: SomeStruct,
}
impl SomeTrait for SomeOtherStruct {
fn get_num(&self) -> i32 {
self.base.get_num()
}
//This dummy struct keeps the default behavior of add_to_num()
}
let the_struct = SomeStruct;
println!("{}", the_struct.add_to_num());
//now we can call the default method using the original struct's data.
println!("{}", SomeOtherStruct { base: the_struct }.add_to_num());
}