GithubHelp home page GithubHelp logo

arturoc / derive_destructure Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michiel-de-muynck/derive_destructure

0.0 2.0 1.0 18 KB

Destructure Rust structs that implement Drop.

License: Apache License 2.0

Rust 100.00%

derive_destructure's Introduction

Crates.io Build Status

derive_destructure

This crate allows you to destructure structs that implement Drop.

If you've ever struggled with error E0509 "cannot move out of type T, which implements the Drop trait" then this crate may be for you.

To use this crate, put this in your lib.rs or main.rs:

#[macro_use]
extern crate derive_destructure;

Then you have 2 ways to use this crate:

Option 1: #[derive(destructure)]

If you mark a struct with #[derive(destructure)], then you can destructure it using

let (field_1, field_2, ...) = my_struct.destructure();

This turns the struct into a tuple of its fields without running the struct's drop() method. You can then happily move elements out of this tuple.

Note: in Rust, a tuple of 1 element is denoted as (x,), not (x).

Option 2: #[derive(remove_trait_impls)]

If you mark your struct with #[derive(remove_trait_impls)], then you can do

let my_struct = my_struct.remove_trait_impls();

The result is a struct with the same fields, but it implements no traits (except automatically-implemented traits like Sync and Send). In particular, it doesn't implement Drop, so you can move fields out of it.

The name of the resulting struct is the original name plus the suffix WithoutTraitImpls. For example, Foo becomes FooWithoutTraitImpls. But you usually don't need to write out this name.

#[derive(remove_trait_impls)] works on enums too.

Example:

#[macro_use]
extern crate derive_destructure;

#[derive(destructure, remove_trait_impls)]
struct ImplementsDrop {
    some_str: String,
    some_int: i32
}

impl Drop for ImplementsDrop {
    fn drop(&mut self) {
        panic!("We don't want to drop this");
    }
}

fn main() {
    // Using destructure():
    let x = ImplementsDrop {
        some_str: "foo".to_owned(),
        some_int: 4
    };
    let (some_str, some_int) = x.destructure();
    // x's drop() method never gets called

    // Using remove_trait_impls():
    let x = ImplementsDrop {
        some_str: "foo".to_owned(),
        some_int: 4
    };
    let x = x.remove_trait_impls();
    // this x doesn't implement drop,
    // so we can move fields out of it
    drop(x.some_str);
    println!("{}", x.some_int);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

derive_destructure's People

Contributors

michiel-de-muynck avatar

Watchers

 avatar  avatar

Forkers

nobodyxu

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.