Git Product home page Git Product logo

Comments (7)

Aaron1011 avatar Aaron1011 commented on May 28, 2024

I was working on something very similar to this, but never quite finished. I can finish it up and open a PR.

from pin-project.

taiki-e avatar taiki-e commented on May 28, 2024

It seems that this can be replaced by wrapping the unpinned field with an Option.

#[pin_project]
enum Example {
    A(#[pin] PinnedValue),
    B(Option<UnpinnedValue>),
    C,
}

#[project]
match self.as_mut().project() {
    Example::A(_) => { /* ... */ },
    Example::B(value @ Some(_)) => { 
        let value = value.take().unwrap();
        // ... 
    },
    Example::B(None) | Example::C => {},
}
self.set(Example::C); // When using this way, whether you should replace self depends on the situation.

from pin-project.

taiki-e avatar taiki-e commented on May 28, 2024

It seems reasonable to add this, given that it increases the size if use Option and that we cannot easily change the public type signature.

from pin-project.

taiki-e avatar taiki-e commented on May 28, 2024

Assigning to @Aaron1011 based on the previous comment (feel free to unassign).

from pin-project.

taiki-e avatar taiki-e commented on May 28, 2024

IIUC, macro needs to generate an additional struct/enum to support this.

@Aaron1011: Do you think it preferable to add a feature like #124 to implement this? (I feel the existing project-attribute-based approach becomes bothered as more types are generated.)

from pin-project.

Diggsey avatar Diggsey commented on May 28, 2024

In order to be panic-safe, it will need to generate code something like:

struct DropInPlaceHelper<T>(*mut T);

impl<T> DropInPlaceHelper<T> {
    fn drop(&mut self) {
        ptr::drop_in_place(self.0);
    }
}

struct OverwriteHelper<T>(*mut T, MaybeUninit<T>);

impl<T> OverwriteHelper<T> {
    fn drop(&mut self) {
        ptr::write(self.0, self.1.assume_init());
    }
}

fn partial_replace(ptr: *mut Foo, new_value: Foo) -> Y {
    let _helper0 = OverwriteHelper(ptr, MaybeUninit::new(new_value));
    let _helper1 = DropInPlaceHelper(&mut (*ptr).pinned_field1);
    let _helper2 = DropInPlaceHelper(&mut (*ptr).pinned_field2);
    let _helper3 = DropInPlaceHelper(&mut (*ptr).pinned_field3);
    ptr::read(&mut (*ptr).unpinned_field)
}

from pin-project.

taiki-e avatar taiki-e commented on May 28, 2024

(Maybe we need to wait for rust-lang/unsafe-code-guidelines#232 discussion?)

from pin-project.

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.