Now that we have an overview of how macros work, let's dive into another macro challenge. When implementing traits for multiple types, you often end up writing very similar code over and over.
In this challenge, you'll create a macro that generates implementations of a trait for multiple types. The goal is to reduce the amount of boilerplate code you need to write.
Write a macro named config_default_impl
that:
ConfigDefault
for that type.Use the macro to implement the ConfigDefault
trait for the following types:
ConnectionTimeout
with a value of 30
.MaxConnections
with a value of 100
.RetryAttempts
with a value of 3
.PostgresPort
with a value of 5432
.MySQLPort
with a value of 3306
.MongoPort
with a value of 27017
.RedisPort
with a value of 6379
.If you're stuck, feel free to check out the hints below:
The syntax for a macro that accepts a type and a value is as follows:
macro_rules! config_default_impl {
($type:ty, $value:expr) => {
// Implementation here
};
}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type_:ty, $value:expr) => { impl ConfigDefault for $type_ { fn get_default() -> Self { Self($value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty,$default:expr)=>{ impl ConfigDefault for $type{ fn get_default()->Self{ Self($default) } } }; // TODO: Implement the macro here}// TODO: Use the macro to implement ConfigDefault for each type Write a macro named config_default_impl that:config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Accepts a type and a default value as arguments.// Generates the implementation of ConfigDefault for that type.// Use the macro to implement the ConfigDefault trait for the following types:// ConnectionTimeout with a value of 30.// MaxConnections with a value of 100.// RetryAttempts with a value of 3.// PostgresPort with a value of 5432.// MySQLPort with a value of 3306.// MongoPort with a value of 27017.// RedisPort with a value of 6379.// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type_name:ty, $default_value:expr) => { impl ConfigDefault for $type_name { fn get_default() -> Self { Self($default_value) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type_name:ty, $default_value:expr) => { impl ConfigDefault for $type_name { fn get_default() -> Self { Self($default_value) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value ) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);config_default_impl!(MySQLPort, 3306);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $value:expr) => { impl $crate::ConfigDefault for $type { fn get_default() -> Self { Self($value ) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);config_default_impl!(MySQLPort, 3306);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($config:ty, $value:expr) => { impl ConfigDefault for $config { fn get_default() -> Self { Self($value) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($tp: ty, $val: expr)=> { impl ConfigDefault for $tp{ fn get_default()-> Self{ Self($val) } } }}config_default_impl!(ConnectionTimeout,30);config_default_impl!(MaxConnections,100);config_default_impl!(RetryAttempts,3);config_default_impl!(PostgresPort,5432);config_default_impl!(MySQLPort,3306);config_default_impl!(MongoPort,27017);config_default_impl!(RedisPort,6379);// TODO: Use the macro to implement ConfigDefault for each type// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $default_value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($default_value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type: ident, $value: expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}pub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($struct_name:ident, $default_value:expr) => { impl ConfigDefault for $struct_name { fn get_default() -> Self { $struct_name($default_value) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ident, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> $type { $type($value) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($_type:ty, $val:expr) => { impl ConfigDefault for $_type { fn get_default() -> Self { Self($val) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } };}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } }}config_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $d:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($d) } } };}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($type:ty, $value:expr) => { impl ConfigDefault for $type { fn get_default() -> Self { Self($value) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}
pub trait ConfigDefault { fn get_default() -> Self;}// These types need default valuespub struct ConnectionTimeout(pub u64);pub struct MaxConnections(pub u32);pub struct RetryAttempts(pub u8);pub struct PostgresPort(pub u16);pub struct MySQLPort(pub u16);pub struct MongoPort(pub u16);pub struct RedisPort(pub u16);#[macro_export]macro_rules! config_default_impl { // TODO: Implement the macro here ($x:ident, $v:expr) => { impl ConfigDefault for $x { fn get_default() -> Self { Self($v) } } }}// TODO: Use the macro to implement ConfigDefault for each typeconfig_default_impl!(ConnectionTimeout, 30);config_default_impl!(MaxConnections, 100);config_default_impl!(RetryAttempts, 3);config_default_impl!(PostgresPort, 5432);config_default_impl!(MySQLPort, 3306);config_default_impl!(MongoPort, 27017);config_default_impl!(RedisPort, 6379);// Example usagepub fn main() { // let's say we have a new struct struct CustomPort(pub u16); // we implement the ConfigDefault trait for CustomPort config_default_impl!(CustomPort, 8080); // when running the `get_default` method, it should return the default value assert_eq!(<CustomPort as ConfigDefault>::get_default().0, 8080);}