Giter Site home page Giter Site logo

Comments (8)

joepio avatar joepio commented on June 23, 2024

Also, with the current approach, I can't call self.store.commit in a Resource method (e.g. resource.save()):

the `commit` method cannot be invoked on a trait objectrustc
storelike.rs(76, 15): this has a `Sized` requirement

from atomic-server.

joepio avatar joepio commented on June 23, 2024

I've posted this question to StackOverlfow. https://stackoverflow.com/questions/64975080/invoking-a-method-on-a-trait-object

from atomic-server.

joepio avatar joepio commented on June 23, 2024

This issue is just the worst... It keeps coming back in all kinds of situations - the sized constraints on the many Storelike methods severely limit where I can call them.

I see no other option than to drastically change the Resource API, and remove the reference to Storelike from Resource. This means that most of the Resource methods will now explicitly require.

from atomic-server.

joepio avatar joepio commented on June 23, 2024

I've tried changing the Resource API, remove the Store reference, but that didn't fix all instances of this issue. The issue happens whenever I call a method that uses Storelike as an argument.

For example, when creating a new Collection::new, Storelike.tpf() is called. This means that Collection::new needs to be compiled for different sizes of Storelike structs. And for this to work, Storelike needs to be sized.

With the refactor I suggested above, I explicitly pass Store to many Resource methods. The result, unfortunately, is that now all the Storelike methods that call Resource methods that use Storelike (self) need to be sized.... E.g. resource.get_shortname(&item, self) is called in Storelike.get_path, which means that now get_path needs a where self is Sized thing.

from atomic-server.

joepio avatar joepio commented on June 23, 2024

Just tried adding :Sized to the trait, and replacing all dyn Storelike with impl Storelike... And so far, it seems to work!

from atomic-server.

joepio avatar joepio commented on June 23, 2024

However, when I try to do this while keeping a reference to Storelike in Resource, things fall apart again:

pub struct Resource<'a> {
    propvals: PropVals,
    subject: String,
    classes: Option<Vec<Class>>,
    // `impl Trait` not allowed outside of function and inherent method return types rustcE0562
    store: &'a impl Storelike,
    commit: CommitBuilder,
}

One possible solution to this, was to use Generics. Just tried that, but I also fail to get that working.

from atomic-server.

joepio avatar joepio commented on June 23, 2024

I kind of fixed it, but it still doesn't feel right. I set a :Sized bound for the entire Storelike trait, which also means that I can't keep a reference to it in other structs, such as Resource. This caused me to refactor quite a bit, and the API got a little worse because of it. Adjusting a resource (e.g. setting a property) now requires explicitly passing the store. Some might prefer it, though, and it does enable multi-store workflows / passing resources from store to store.

Anyway - I can now do all the things i needed to do. Use impl Storelike instead of dyn Storelike if you want to stay out of trouble. Closing.

from atomic-server.

joepio avatar joepio commented on June 23, 2024

Well, it turns out setting :Sized also comes with some serious limitations. I'm working on a plugin #73 system, which includes structs that contain functions that refer to Storelike. And that's a problem, because Storelike is sized, and that means I can't use &impl Storelike in that function.

I think there are two solutions to this:

  • Get rid of one of the implementations (which will be the in-memory one)
  • Move all the error-causing logic from the trait. Might involve some code duplication, but I get to keep both implementations.

from atomic-server.

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.