Type specialization

Hello world. I a would like a function's signature to vary from one implementation of its parent trait to another. Is it possible ?

Something like...

pub trait Serialize {
	fn binify<ByteArray>(self) -> ByteArray;
	fn structify<ByteArray>(raw: ByteArray) -> Self; 

impl Serialize for u8 {
	fn binify(self) -> [u8; 1] {
		return [self];
	fn structify(raw: &[u8; 1]) -> Self {
		return raw[0];

impl Serialize for u16 {
	fn binify(self) -> [u8; 2] {
		return self.to_be_bytes();
	fn structify(raw: &[u8; 2]) -> Self {
		return u16::from_be_bytes(*raw);

Traitless function overloading would be so much simpler ! :tired_face::weary:

See Advanced Traits - The Rust Programming Language

You can add a type ByteArray to the trait and then set it to different types in each implementation.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.