These are chat archives for rust-lang/rust

20th
Sep 2018
vdagonneau
@vdagonneau
Sep 20 2018 08:56
Hi everyone, I have a noob question. I do not understand how to do something in Rust. I made a snippet on the Rust Playground to illustrate what I do not understand. Is it the right channel to be talking about that ?
Here is the link: https://play.rust-lang.org/?gist=100aa10cc9f2178cfa9dc05125c682c8&version=stable&mode=debug&edition=2015 I understand why it does not work. I would like to know which way I am supposed to handle that situation in Rust.
Michael(LAI)
@Michael-Lfx
Sep 20 2018 13:46
Hi everyone. I have a noob question too. How to rewrite the following C++ class in Rust properly? Especially those fields such as ResourceManager * mResourceManager, StateManager mStateManager, should I make them pub(crate) or pub directly or provide getter/setter for them?
class Context {

private:
// ------------
    const
    vulkanAPI::vkContext_t *                    mVkContext;
// ------------
    StateManager                                mStateManager;
    ResourceManager *                           mResourceManager;
    CacheManager *                              mCacheManager;
    ShaderCompiler *                            mShaderCompiler;
    vulkanAPI::Pipeline *                       mPipeline;
    vulkanAPI::ClearPass *                      mClearPass;
    vulkanAPI::CommandBufferManager            *mCommandBufferManager;

// ------------
    void        *                               mWriteSurface;
    void        *                               mReadSurface;
    BufferObject*                               mExplicitIbo;
    Framebuffer *                               mWriteFBO;

    Framebuffer *                               mSystemFBO;
    vector<Texture *>                           mSystemTextures;
// ------------

    Shader        *GetShaderPtr(GLuint shader);
    ShaderProgram *GetProgramPtr(GLuint program);

    Framebuffer   *CreateFBOFromEGLSurface(EGLSurfaceInterface *eglSurfaceInterface);
    Framebuffer   *InitializeFrameBuffer(EGLSurfaceInterface *eglSurfaceInterface);
    Framebuffer   *AllocatePBufferTexture(EGLSurfaceInterface *eglSurfaceInterface);
    Texture       *CreateDepthStencil(EGLSurfaceInterface *eglSurfaceInterface);

    inline  StateManager    *GetStateManager(void)      { FUN_ENTRY(GL_LOG_TRACE); return &mStateManager; }

    // lots of stuff...
};
Denis Lisov
@tanriol
Sep 20 2018 14:06
@Michael-Lfx Well, if you're rewriting, you can probably keep them private :-) however, are you sure rewriting it 1-to-1 is a good approach?
I hope you do understand the ownership relations in this class :-)
Michael(LAI)
@Michael-Lfx
Sep 20 2018 14:13
@tanriol Not 1:1, I want to keep the Rust version to the same functionality with the C++ one but I don't how to do it properly.
Lyle Mantooth
@IslandUsurper
Sep 20 2018 14:27
@vdagonneau , probably this way: https://play.rust-lang.org/?gist=f4ff8d3e446e65aaef233224bc5a414a&version=stable&mode=debug&edition=2015 This does end up looping twice, but the key seems to be moving the borrowing code away from the assigning code.
With usize, though, it's hard to tell if it actually is taking references to the items rather than Copying them.
trsh
@trsh
Sep 20 2018 14:28
How can I fix this:
let q = format!("limit=1&ids={}", id_str);

    Box::new(
        rest_proxy_manual("orders", &q, db)
            .map_err(|e| vec![e.to_string()])
            .and_then(|r| ok(vec![Some(r.0)])),
    )
error[E0597]: `q` does not live long enough
  --> manu/src/applications/api/gqls/doc_templates/workers/shopify_order.rs:81:38
   |
81 |         rest_proxy_manual("orders", &q, db)
   |                                      ^ borrowed value does not live long enough
...
85 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...
Can I make q some-have in static lt?
Denis Lisov
@tanriol
Sep 20 2018 14:52
@trsh No nice way. You should make rest_proxy_manual return a static value instead, probably by cloning the string internally or by receiving an owned string.
trsh
@trsh
Sep 20 2018 15:02
yeah
vdagonneau
@vdagonneau
Sep 20 2018 15:45
@IslandUsurper Thank you, I had already the idea of having two for loops but that seemed very ugly. I guess I don't have a choice ...