Skip to content
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

Simplify the unix Weak functionality #58442

Merged
merged 2 commits into from
Feb 24, 2019
Merged

Simplify the unix Weak functionality #58442

merged 2 commits into from
Feb 24, 2019

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Feb 13, 2019

  • We can avoid allocation by adding a NUL to the function name.
  • We can get Option<F> directly, rather than aliasing the inner AtomicUsize.

If we add a terminating NUL to the name in the `weak!` macro, then
`fetch()` can use `CStr::from_bytes_with_nul()` instead of `CString`.
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 13, 2019
@Centril
Copy link
Contributor

Centril commented Feb 23, 2019

r? @RalfJung

mem::transmute::<&AtomicUsize, Option<&F>>(&self.addr)
match self.addr.load(Ordering::SeqCst) {
0 => None,
addr => Some(mem::transmute_copy::<usize, F>(&addr)),
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment saying that the sizes are equal because we asserted this earlier. (Took me a bit to see that. As I said, I don't know this code and feel somewhat uncomfortable reviewing it.)

use marker;
use mem;
use sync::atomic::{AtomicUsize, Ordering};

macro_rules! weak {
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
static $name: ::sys::weak::Weak<unsafe extern fn($($t),*) -> $ret> =
::sys::weak::Weak::new(stringify!($name));
::sys::weak::Weak::new(concat!(stringify!($name), '\0'));
)
}
Copy link
Member

@RalfJung RalfJung Feb 23, 2019

Choose a reason for hiding this comment

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

Shouldn't something about this abstraction be unsafe? Like, either the constructor or get or so?

Seems we are using it only with unsafe fn, but the Weak type itself can't know that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I think it would make sense for the constructor to be unsafe, which the macro can hide knowing this is unsafe fn. All uses do go through this macro already though.

@RalfJung
Copy link
Member

The concurrency aspect looks fine, but someone who knows the library around this should review. This doesn't look like a safe abstraction to me, so it has to be reviewed knowing how it is used.

@Mark-Simulacrum
Copy link
Member

r? @alexcrichton perhaps?

@alexcrichton
Copy link
Member

@bors: r+

Thanks @cuviper!

@bors
Copy link
Contributor

bors commented Feb 23, 2019

📌 Commit 33d80bf has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 23, 2019
Centril added a commit to Centril/rust that referenced this pull request Feb 24, 2019
Simplify the unix `Weak` functionality

- We can avoid allocation by adding a NUL to the function name.
- We can get `Option<F>` directly, rather than aliasing the inner `AtomicUsize`.
Centril added a commit to Centril/rust that referenced this pull request Feb 24, 2019
Simplify the unix `Weak` functionality

- We can avoid allocation by adding a NUL to the function name.
- We can get `Option<F>` directly, rather than aliasing the inner `AtomicUsize`.
bors added a commit that referenced this pull request Feb 24, 2019
Rollup of 6 pull requests

Successful merges:

 - #57364 (Improve parsing diagnostic for negative supertrait bounds)
 - #58183 (Clarify guarantees for `Box` allocation)
 - #58442 (Simplify the unix `Weak` functionality)
 - #58454 (Refactor Windows stdio and remove stdin double buffering )
 - #58511 (Const to op simplification)
 - #58642 (rustdoc: support methods on primitives in intra-doc links)

Failed merges:

r? @ghost
@bors bors merged commit 33d80bf into rust-lang:master Feb 24, 2019
@cuviper cuviper deleted the unix-weak branch March 27, 2019 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants