Giter VIP home page Giter VIP logo

Comments (2)

dev-ardi avatar dev-ardi commented on September 23, 2024 1

The main problem is that you have made your trait generic when it doesn't need to be. You can just make the decode function generic so that you don't tie it to any specific type like this:

fn main() {
    pub trait Encodable {
        fn encode(&self) -> Vec<u8>;
    }

    pub trait LengthComputable {
        fn len(&self) -> usize;
    }

    pub trait Decodable {
        fn decode<D: AsRef<[u8]>>(data: D) -> Self;
    }

    #[repr(u8)]
    #[derive(Copy, Clone)]
    pub enum DataType {
        Ack = 0x00,
        ParamSet = 0x04,
        VehicleCtrl = 0x05,
        BlueKey = 0x06,
        BlueKeyV2 = 0x07,
        ParamQuery = 0x0A,
        DataReport = 0x0C,
        FileTrans = 0x0F,
    }
    impl Encodable for DataType {
        fn encode(&self) -> Vec<u8> {
            vec![*self as u8]
        }
    }
    impl LengthComputable for DataType {
        fn len(&self) -> usize {
            std::mem::size_of::<Self>()
        }
    }
    impl Decodable for DataType {
        fn decode<D: AsRef<[u8]>>(data: D) -> Self {
            todo!()
        }
    }

    pub struct DataUnit<T>
    where
        T: Encodable + LengthComputable,
    {
        pub(crate) data_type: DataType,
        pub(crate) data: T,
    }

    impl<T> LengthComputable for DataUnit<T>
    where
        T: Encodable + LengthComputable,
    {
        fn len(&self) -> usize {
            self.data_type.len() + self.data.len()
        }
    }

    impl<T> Encodable for DataUnit<T>
    where
        T: Encodable + LengthComputable,
    {
        fn encode(&self) -> Vec<u8> {
            todo!()
        }
    }

    impl<T> Decodable for DataUnit<T>
    where
        T: Encodable + Decodable + LengthComputable,
    {
        fn decode<D: AsRef<[u8]>>(data: D) -> Self {
            let mut index = 0;
            let mut size = 0;

            size = std::mem::size_of::<DataType>();

            let data = Vec::from(data.as_ref());
            let data = &data[index..index + size];
            let data_type = DataType::decode(data);

            let data = &data[index..index + size];
            let data = T::decode(data);

            DataUnit { data_type, data }
        }
    }
}

However you're better off asking these kinds of questions in https://users.rust-lang.org/

from rust.

zhuyu4839 avatar zhuyu4839 commented on September 23, 2024

Thanks

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.