Skip to content

A better way to denote noncopyable types #4069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bstrie opened this issue Nov 29, 2012 · 10 comments
Closed

A better way to denote noncopyable types #4069

bstrie opened this issue Nov 29, 2012 · 10 comments
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@bstrie
Copy link
Contributor

bstrie commented Nov 29, 2012

In the past the way to tell the compiler that a struct wasn't copyable was to stick an empty drop {} block at the end of it. This was hacky and non-obvious, but at least it was concise. But with the Drop trait this now expands to impl Foo : Drop { fn finalize(&self) {} }, which is rather unfortunate. A #[noncopyable] attribute that expanded to the above would be clearer and less verbose.

@brson
Copy link
Contributor

brson commented Nov 29, 2012

There's also util::NonCopyable which is a type that can be put inside a struct to make it noncopyable.

There may also be other solutions to this like:

trait Drop {
    fn finalize() { /* default impl */ }
}
trait NonCopyable: Drop;

// Here I'm using imaginary syntax to indicate that MyType impls both NonCopyable and all the supertraits of NonCopyable
impl MyType: NonCopyable(*);

@brson
Copy link
Contributor

brson commented Nov 29, 2012

Actually, with a default impl of finalize you could just write impl MyType: Drop, though the NonCopyable trait would be clearer.

@bstrie
Copy link
Contributor Author

bstrie commented Nov 29, 2012

Changed issue title to be more general

@bblum
Copy link
Contributor

bblum commented Dec 2, 2012

incidentally, it makes me queasy that util::NonCopyable takes a byte of memory, because of the unit(/nil) inside.

@brson
Copy link
Contributor

brson commented Dec 2, 2012

This could also work, depending on what generic impls are legal

trait Drop {
    fn finalize(&self);
}

trait NonCopyable;

impl <T: NonCopyable> T: Drop {
    fn finalize(&self) { }
}

@bstrie
Copy link
Contributor Author

bstrie commented Apr 29, 2013

Now that we have #[unsafe_destructor], it seems like there's a precedent for making this an attribute rather than an impl. Perhaps we could also consider #[deriving(Noncopyable)].

@bstrie
Copy link
Contributor Author

bstrie commented Jul 2, 2013

Now that util::Noncopyable no longer takes up space, it's the preferred way to denote these types. Closing.

@bstrie bstrie closed this as completed Jul 2, 2013
@mzabaluev
Copy link
Contributor

How to initialize a structure that contains an util::NonCopyable member?
This is the last wart for me in what otherwise would be a good solution.

@thestinger
Copy link
Contributor

struct { non_copyable: std::util::NonCopyable, y: 5 }

Although right now, unit structs are buggy and can't be constructed cross-crate.

@mzabaluev
Copy link
Contributor

2013/7/5 Daniel Micay [email protected]

Although right now, unit structs are buggy and can't be constructed
cross-crate.

Yeah, that's what I apparently ran into.

Thank you,
Mikhail

RalfJung added a commit to RalfJung/rust that referenced this issue Dec 21, 2024
…r_localtime_r

localtime_r: deduplicate timezone name allocation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants