Code Monkey home page Code Monkey logo

Comments (9)

andybarron avatar andybarron commented on September 26, 2024

Ah, I hadn't even thought of this use case! Great point. I'll fix this soon, or feel free to open a PR if you like - it should be a pretty simple change.

from app-dirs-rs.

andybarron avatar andybarron commented on September 26, 2024

Wait, I remember why I did this now. You can't create a thread-safe, read-only const/static instance with String, but you can with &'static str. I really didn't want to require folks to pass around the AppInfo instance or have to pull in lazy_static as a dependency...

I wonder how we can solve this issue and yours? I think they are both valid.

from app-dirs-rs.

icefoxen avatar icefoxen commented on September 26, 2024

It appears that using std::borrow::Cow makes it possible, though it's not terribly ergonomic and I'm still not 100% sure how it interacts with threading. See icefoxen@52c69a9 The canonical next step seems like it would be "add a macro to make it a bit prettier" but it seems like overkill...

from app-dirs-rs.

andybarron avatar andybarron commented on September 26, 2024

Ooh, I bet I could use a simple trait with two methods (for author and app name) that return String, and provide the default struct as an implementation with &'static str fields that return String copies. So we can keep the ergonomic default implementation while allowing libs like yours to do different things :)

from app-dirs-rs.

icefoxen avatar icefoxen commented on September 26, 2024

That would be wonderful. <3

from app-dirs-rs.

icefoxen avatar icefoxen commented on September 26, 2024

Ok, trying to implement a PR for this and I've realized I don't understand exactly what you mean. Something like this?

trait AppInfoT {
    fn name(&self) -> String;
    fn author(&self) -> String;
}

impl AppInfoT for AppInfo {
    fn name(&self) -> String {
        self.name.to_string()
    }
    fn author(&self) -> String {
        self.author.to_string()
    }
}

from app-dirs-rs.

andybarron avatar andybarron commented on September 26, 2024

More like this:

trait AppInfo {
    fn name(&self) -> &str;
    fn author(&self) -> &str;
}

struct StaticAppInfo {
    name: &'static str,
    author: &'static str,
}

struct DynamicAppInfo {
    name: String,
    author: String,
}

impl AppInfo for StaticAppInfo {
    fn name(&self) -> &str {
        self.name
    }
    fn author(&self) -> &str {
        self.author
    }
}

impl AppInfo for DynamicAppInfo {
    fn name(&self) -> &str {
        &self.name
    }
    fn author(&self) -> &str {
        &self.author
    }
}

Returning str instead of String avoids unnecessary cloning/allocation.

from app-dirs-rs.

icefoxen avatar icefoxen commented on September 26, 2024

Finally got around to implementing this in icefoxen@71ba239 . If you like it I'll update the docs and make a PR. It breaks the API, which is annoying, and I don't entirely like the StaticAppInfo name, but I messed around with other ways of doing the same thing in icefoxen@09a2440 and I'm not sure any of them are better.

from app-dirs-rs.

TruputiGalvotas avatar TruputiGalvotas commented on September 26, 2024

Hello,

so I've made a change, before checking that there was an issue about this, please see #22 I've also added a comment about #22 vs #23 in there.

from app-dirs-rs.

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.