I am just learning rust, coming from Python. I am trying to write a Modbus Server as my first project. In Modbus, a client can request data of certain types through the use of a function code field. I am trying to have a function that reads the function code field from the request and returns the function which can handle that function code. That function is called get_modbus_function in the lib.rs file linked below. I have functions implemented for two of the possible function codes: read_coils and read_holding registers.
The problem I am having is understanding how to make this generic in order to handle the different types of read_coils and read_holding_registers.
Here is the compiler errors:
Compiling playground1 v0.1.0 (file:///home/keith/code/rust-playground)
src/lib.rs:84:32: 84:42 error: mismatched types:
expected `fn(ReadRequest, &collections::vec::Vec<T>) -> core::result::Result<U, ModbusError>`,
found `fn(ReadRequest, &collections::vec::Vec<bool>) -> core::result::Result<ModbusResponse<bool>, ModbusError> {read_coils}`
(expected type parameter,
found bool) [E0308]
src/lib.rs:84 READ_COILS => Box::new(read_coils),
^~~~~~~~~~
src/lib.rs:84:32: 84:42 help: run `rustc --explain E0308` to see a detailed explanation
src/lib.rs:85:44: 85:66 error: mismatched types:
expected `fn(ReadRequest, &collections::vec::Vec<T>) -> core::result::Result<U, ModbusError>`,
found `fn(ReadRequest, &collections::vec::Vec<u16>) -> core::result::Result<ModbusResponse<u16>, ModbusError> {read_holding_registers}`
(expected type parameter,
found u16) [E0308]
src/lib.rs:85 READ_HOLDING_REGISTERS => Box::new(read_holding_registers),
^~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:85:44: 85:66 help: run `rustc --explain E0308` to see a detailed explanation
src/lib.rs:86:23: 86:33 error: mismatched types:
expected `fn(ReadRequest, &collections::vec::Vec<T>) -> core::result::Result<U, ModbusError>`,
found `fn(ReadRequest, &collections::vec::Vec<bool>) -> core::result::Result<ModbusResponse<bool>, ModbusError> {read_coils}`
(expected type parameter,
found bool) [E0308]
src/lib.rs:86 _ => Box::new(read_coils)
^~~~~~~~~~
src/lib.rs:86:23: 86:33 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 3 previous errors
Could not compile `playground1`.
I am not sure if this is possible or not. I have read the book and tried searching online. I am not exactly sure what to search for. Any pointers would be greatly appreciated.