Comments (2)
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.
Thanks
from rust.
Related Issues (20)
- ICE when triggering a malformed `diagnostic::on_unimplemented` across crate boundaries (`DefId(20:3 ~ ice_lib_test[60d0]::Test)` isn't local) HOT 1
- Ludicrous build time for `keshvar` crate in `release` mode HOT 4
- Imports of legacy modules with numeric constants do not get flagged as unused when the corresponding type is used in the code HOT 1
- When using `Box<self>` in first method parameter, the error output provides the wrong type in help message HOT 1
- `IoSlice`: the `Deref` impl is too restrictive on the lifetime of the slice, an alternative is needed. HOT 1
- Trait coherency regression in 1.78 HOT 3
- Type inference not working well with ptr mutability HOT 2
- GetSystemTimePreciseAsFileTime HOT 3
- Subpar documentation for ptr-to-ref conversion methods HOT 5
- Compiler Panics ,when I use Ractor Framework HOT 5
- 1.78.0 & WASM - panicked at dlmalloc.rs: assertion failed: psize <= size + max_overhead HOT 5
- False positives from invalid_reference_casting HOT 6
- Nested argument mismatch could suggest moving extra parameter outside parenthesis HOT 3
- Explain the difference between Once(Lock|Cell) and Lazy(Lock|Cell) in the docs HOT 2
- ICE: `Result::unwrap()` on an `Err` value: Sorts(ExpectedFound { ... } HOT 1
- rustfmt eats attributes in inline const blogs HOT 2
- Sometimes 'Σ' is not handled correctly in str.to_lowercase() HOT 2
- Compiler Crash with "Hello world" on latest nightly (aarch64) HOT 2
- docs: Hermit targets link to wrong GitHub org
- Unable to write blanket impl for subtrait on RPITIT trait HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.