Giter Club home page Giter Club logo

Comments (9)

MathiasKoch avatar MathiasKoch commented on August 22, 2024 1

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.

eugene2k avatar eugene2k commented on August 22, 2024 1

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.

eugene2k avatar eugene2k commented on August 22, 2024

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.

MathiasKoch avatar MathiasKoch commented on August 22, 2024

@eugene2k That would be awesome! But in my case, it should be alloc free, and Box<> is not :(

from atat.

eugene2k avatar eugene2k commented on August 22, 2024

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.

eugene2k avatar eugene2k commented on August 22, 2024

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 Boxes.

from atat.

Michael-F-Bryan avatar Michael-F-Bryan commented on August 22, 2024

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.

MathiasKoch avatar MathiasKoch commented on August 22, 2024

@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.

tl8roy avatar tl8roy commented on August 22, 2024

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)

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.