How to overload method in Rust (amount of args differ).
Tags: rust, programming
Published: 2021-10-16 19:04:23

---
title: How to overload method in Rust (amount of args differ).
published: true
description: If you want to overload methods like `fn Sample::new(arg0, arg1)` and `fn Sample::new(arg)`, this post might be your help.
tags: rust, programming,
//cover_image: https://direct_url_to_image.jpg
---

## Adapt version

```
$ rustc --version
rustc 1.55.0 (c8dfcfe04 2021-09-06)
```

If you want to overload methods that have different amount of args likes following

```rust
// Constructor with 2 args.
fn new(arg0, arg1) -> Self {
    ...
}

// Constructor with 1 arg.
fn new(arg) -> Self {
    ...
}
```

*Macro* is your better friend.

So macro can be used for pseud-overloading.

## Pseud-overloading with macro.

```rust
struct SampleStruct {
    ip_address: IpAddr,
    port_number: u16,
    sample_socket: socket2::Socket,
}

macro_rules! SampleStruct_new {
    ($str_ip:expr , $num_port:expr) => ({
        let ip: IpAddr = $str_ip.parse::<IpAddr>()
            .unwrap_or_else( |_| { panic!("`address` MUST be an IPv4 address or IPv6 address.") });
        
        SampleStruct {
            ip_address: ip,
            port_number: $num_port,
            sample_socket: Socket::new(
                if ip.is_ipv4() { Domain::IPV4 } else { Domain::IPV6 },
                Type::DGRAM,
                Some(Protocol::UDP)
            ).unwrap()
        }
    });

    ($obj_SocketAddr:expr) => ({
        SampleStruct {
            ip_address: $obj_SocketAddr.ip(),
            port_number: $obj_SocketAddr.port(),
            sample_socket: Socket::new(
                if $obj_SocketAddr.ip().is_ipv4() { Domain::IPV4 } else { Domain::IPV6 },
                Type::DGRAM,
                Some(Protocol::UDP)
            ).unwrap()
        }
    });
}
```

Then it uses as following:

```rust
fn main() {
    let foo = SampleStruct_new!("127.0.0.1", 12345);
    println!("IP address = {}, Port = {}", foo.ip_address.to_string(), foo.port_number);

    let ipv6_sock_addr = SocketAddr::new("::1".parse::<IpAddr>().unwrap(), 12345);
    let bar = SampleStruct_new!(ipv6_sock_addr);
    println!("IP address = {}, Port = {}", bar.ip_address.to_string(), bar.port_number);
}
```

To see a sample code, go [Rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3d35da870934e08ba41e787237df5ede)


## Generics with placeholder.

*Generics with placeholder* is also your frind. `_:()` in `impl` block means "this is unused argument".

```rust
trait SampleTrait<T, O> {
    fn new(address:T, port:O) -> SampleStruct;
}

impl SampleTrait<&str, u16> for SampleStruct {
    fn new(address: &str, port: u16) -> Self {
        let addr = address.to_string().parse::<IpAddr>()
            .unwrap_or_else( |_| { panic!("`address` MUST be an IPv4 address (dotted-decimal form) or an IPv6 address.") });
        
        return SampleStruct {
            ip_address: addr,
            port_number: port,
            sample_socket: Socket::new(
                if addr.is_ipv4() { Domain::IPV4 } else { Domain::IPV6 },
                Type::DGRAM,
                Some(Protocol::UDP)
            ).unwrap()
        }
    }
}

impl SampleTrait<SocketAddr, ()> for SampleStruct {
    fn new(address: SocketAddr, _:()) -> Self {
        return SampleStruct {
            ip_address: address.ip(),
            port_number: address.port(),
            sample_socket: Socket::new(
                if address.ip().is_ipv4() { Domain::IPV4 } else { Domain:: IPV6 },
                Type::DGRAM,
                Some(Protocol::UDP)
            ).unwrap()
        }
    }
}
```

When calling `fn new(address: SocketAddr, _:())`, the `()` MUST be passed to unused args. So it seems slightly ugly in this way.

```rust
fn main() {
    let foo = SampleStruct::new("127.0.0.1".to_string(), 12345);
    println!("IP address = {}, Port = {}", foo.ip_address.to_string(), foo.port_number);

    let ipv6_sock_addr = SocketAddr::new("::1".parse::<IpAddr>().unwrap(), 12345);
    let baz = SampleStruct::new(ipv6_sock_addr, ());
    println!("IP address = {}, Port = {}", baz.ip_address.to_string(), baz.port_number);
}
```

To see a sample code, go [Rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=958e0fb6ef45fd918c0011555d1f1add)