Comments (9)
This is great! Keep the feedback coming, and i will do my best to improve the crate based on it. I am still fairly new to rust, so all feedback is awesome!
from atat.
How would i return a slice from a function where i would need to format the contents of the slice based on input arguments?
Returning a &str is not a hard rule. If your function takes arguments and creates a string out of them, then unless you also store this string internally you should return the string. If the function stores the string internally, then you should consider having it return a reference to the stored string instead of a clone of the string. The same goes for slices.
from atat.
What you want is dynamic dispatch. impl ATCommand
is for static dispatch, Box<dyn ATCommand> would be for dynamic dispatch. The current version of rust book doesn't seem to talk about trait objects, so you have to refer to the previous version: https://doc.rust-lang.org/1.30.0/book/first-edition/trait-objects.html
from atat.
@eugene2k That would be awesome! But in my case, it should be alloc free, and Box<>
is not :(
from atat.
Heapless has a Pool
structure which allocates a generic Box
. Std's Box
is not unique in being able to use trait objects.
from atat.
Actually, I think I might be wrong here because it doesn't look like Heapless's Box
works with unsized data. So, what you can do is (depending on your use case) either store &dyn ATCommand
objects in the queue or *const dyn ATCommand
while storing the real objects in Box
es.
from atat.
These are just some thoughts I had while skimming through the proposed API, feel free to scrutinise or disregard them completely 🙂
You probably shouldn't add things like N: ArrayLength<u8>
parameter to the trait methods. This means the types are no longer object safe, which makes it a pain when you need to handle different types of commands. Even if you don't have Box
, you could create commands on the stack (or as static variables) and store them in an array/Vec
(e.g. &[&Dyn ATCommand]
).
It also feels funny to be returning a String
from get_cmd()
, normally I'd expect a getter to return a reference.
For that matter, why does a command expose it's internals in such a way? Although Rust isn't your stereotypical OO language, I think this interface could benefit from Martin Fowler's Tell, Don't Ask.
Why does the response handle owned data (Vec<String<T>, L>
) instead of borrowed data? If you pass a &str
from some buffer the response is written to, the implementor can call .lines()
on to extract the response lines and you won't be copying hundreds of bytes around (just because we're not using the heap, doesn't mean these helpless data structures are cheap!) just to read a response.
Passing in heapless
types as parameters doesn't really add much over normal slices unless you want the user to mutate things. Even when parsing data it's quite common to take a &str
or &[u8]
and return a tuple of Response
and the number of bytes read (E.g. fn parse(data: &str) -> Result<(R, usize), ParseError>
). That way the caller can remove the appropriate number of bytes from its buffer, and it removes a lot of the cognitive overhead associated with type-level integers and generics.
from atat.
@Michael-F-Bryan
Taking your points one at a time, how would i handle
It also feels funny to be returning a String from get_cmd(), normally I'd expect a getter to return a reference.
Correctly in rust?
I assume what you mean by reference is that it should return borrowed data &str
, which i initially tried, but couldn't make work due to my very limited experience with lifetimes.
How would i return a slice from a function where i would need to format the contents of the slice based on input arguments?
eg.
fn get_cmd(&self) -> &str {
match self {
Command::Example { a } => ** String slice containing 'a'**
}
}
I feel like if i could work out stuff like that, i would probably be able to remove a lot of owned data (String, Vec) and also a lot of the N: ArrayLength<u8>
parameters that follows from them.
from atat.
ufmt needs to write to a ?Sized
type, so that is probably the starting point.
You could pass a buffer through get_cmd
and just return Ok(()). That is not that unusual in no_std.
I am fairly sure I can make my macro work with anything once I have the traits defined (and an example)
from atat.
Related Issues (20)
- Add support for automatic command retry
- Anchoring parsing issue
- Adding esp-at-nal to supported crates HOT 6
- Question - A response coming back as hex? HOT 13
- AT command length computation does not take string quotes into account HOT 1
- Question: Enum for "Success == 0, Failure for everything else" HOT 1
- custom handlers in AtDigester should support returning nom::Err::Incomplete
- Doing a simple modem example with ATAT HOT 16
- ATI response cannot be parsed as String, but can as Bytes HOT 32
- URC parser does not allow colon HOT 5
- Subscriber does not feature the "Send" Trait HOT 2
- Null Parser HOT 3
- URC with length and binary content HOT 7
- Support for commands with both quoted and unquoted strings
- CmeError and CmsError not exported? HOT 1
- URC and response conflict for same command HOT 8
- Parsing AT+CMGR response, falling on empty ,, to option and trailing string HOT 7
- Unhandled URC causes URC to stop working HOT 3
- Response without leading '\r\n' is eaten by echo detector HOT 2
- Response with \r\r\n confuses URC Parser 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 atat.