Skip to content

c_vec: Modernize #10736

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

Merged
merged 1 commit into from
Dec 3, 2013
Merged

c_vec: Modernize #10736

merged 1 commit into from
Dec 3, 2013

Conversation

Blei
Copy link
Contributor

@Blei Blei commented Nov 30, 2013

Controversial change: make the constructors safe, but instead mark the
setters and getters as unsafe. This follows the tradition that construction
of *T is safe, but the access is unsafe instead.

*
* Fails if `ofs` is greater or equal to the length of the vector
*/
pub unsafe fn get(&self, ofs: uint) -> T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably be:

pub unsafe fn get<'a>(&'a self, idx: uint) -> &'a T { ... }

and avoid the compulsory .clone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, true. Will change, thanks!

@Blei
Copy link
Contributor Author

Blei commented Nov 30, 2013

Fixed, ready for next round of reviews.

@alexcrichton
Copy link
Member

I'm personally not a fan of moving the unsafe-ness to the setters/getters instead of the constructor. This class is supposed to be a safe interface around dealing with C vectors. The only unsafe operation is constructing a class saying that "this unsafe memory is actually valid". Once you have made that statement, you should no longer have to make the statement that you're willing to accept unsafe code.

@Blei
Copy link
Contributor Author

Blei commented Dec 2, 2013

Thanks for your remarks everyone. Rewritten parts of it, rebased and squashed. See commit message for overview of the current version :)

Generally use more modern constructs (such as using `CVec::new()` as
constructor and move to more method usage).

Potentially controversial changes:
* change `get()` to return a reference instead of cloning
* remove `set()`, add `get_mut()` instead
* add an `unwrap()` method that destroys the CVec without running any
  associated destructor
@Blei
Copy link
Contributor Author

Blei commented Dec 2, 2013

Pushed a new version

bors added a commit that referenced this pull request Dec 3, 2013
Controversial change: make the constructors safe, but instead mark the
setters and getters as unsafe. This follows the tradition that construction
of *T is safe, but the access is unsafe instead.
@bors bors closed this Dec 3, 2013
@bors bors merged commit 870fb7d into rust-lang:master Dec 3, 2013
@Blei Blei deleted the fix_c_vec branch December 3, 2013 05:49
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 20, 2023
initial clippy::ref_pattern implementation

This implements a new lint that discourages the use of the `ref` keyword as outlined in rust-lang#9010. I think there are still some things to improve about this lint, but I need some feedback before I can implement those.

- [x] ~~Maybe it's desirable that a quick fix is listed too, instead of a generic `avoid using the ref keyword` lint.~~
- [x] `let ref x = y` already has a lint (`clippy::toplevel_ref_arg`). This implementation will report this too, so you get two lints for the same issue, which is not great. I don't really know a way around this though.
- [X] The dogfood test is currently failing locally, though I ran `cargo clippy -- -D clippy::all -D clippy::pedantic` and got no output, so I'm not sure why this is.

Any help with these would be greatly appreciated.

fixes rust-lang#9010
changelog: [`ref_pattern`]: newly added lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants