Code Monkey home page Code Monkey logo

Comments (3)

peter-kehl avatar peter-kehl commented on June 24, 2024

I'm reading https://doc.rust-lang.org/nightly/core/pin/index.html, it will take me a while.

My goal was: Something like https://doc.rust-lang.org/nightly/core/pin/index.html#example-self-referential-struct, but not in a Box (to be no_std & heapless). But I may see a way; if I need more help, I'll ask.

from selfie.

prokopyl avatar prokopyl commented on June 24, 2024

Hi, and thanks for your interest in Selfie!

Even though you closed the issue I'm still going to answer your initial question, I think this will help, and also I believe it highlights weaknesses in the documentation.

For the part about putting a Selfie in a struct, here is how it would look like (using a Box for simplicity):

pub struct Outer {
    inner: Selfie<'static, Box<[u8; 8]>, Ref<[u8]>>,
}

The generic lifetime of a Selfie (or SelfieMut) is actually the lifetime of the "owned" pointer P. If you actually own what's behind P, then it's just a 'static lifetime.

You can also move that struct (both Outer and the inner selfie field) freely: the Box here is what's guaranteeing that your data isn't moving (or any pinned pointer).

As for a no_std & heapless solution, Selfie is not actually the one that provides the non-movable guarantee of the data, that's the role of the pointer type P. Many pointer types provide this capability (behind the StableDeref trait), but if you're comfortable with your Selfie being non-static, a simple & reference will also work as a stable pointer:

pub struct Outer<'a> {
    inner: Selfie<'a, &'a [u8; 8], Ref<[u8]>>,
}

fn do_thing_with_outer(outer: Outer) { todo!() }

fn main() {
    let not_moving_data = [0; 8];
    let outer = Outer {
        inner: Selfie::new(Pin::new(&not_moving_data), |data| &data[4..])
    };

    // Outer can be moved here, no problem as long as not_moving_data is still alive
    do_thing_with_outer(outer);
}

Hope this helps! 🙂

from selfie.

peter-kehl avatar peter-kehl commented on June 24, 2024

Thank you. I realized that I don't need lazy/interior mutability for now. But I'll remember Selfie.

Thank you also for MIRI GitHub actions config. I'm using it with my project to check unsafe.

from selfie.

Related Issues (2)

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.