diggsey / rust-field-offset Goto Github PK
View Code? Open in Web Editor NEWSafe pointer-to-member functionality for rust
License: Apache License 2.0
Safe pointer-to-member functionality for rust
License: Apache License 2.0
This code is highly problematic:
pub unsafe fn new<F: for<'a> FnOnce(&'a T) -> &'a U>(f: F) -> Self {
// Construct a "fake" T. It's not valid, but the lambda shouldn't
// actually access it (which is why this is unsafe)
let x = mem::zeroed();
If T
is, for example, a struct containing reference type, then this causes UB by initializing a reference with 0. The comment seems to say that it is okay to do this as long as x
is not accessed; that is not correct.
That said, there currently is no correct way to do what you want. This bug can only really be fixed once MaybeUninit
gets stabilized.
(I also feel really uneasy with the extend to which this relies on type inference, but YMMV.^^)
I cannot seem to figure out how to get the offset of fields in a enum variant. I tried Enum::Variant => field
but that seems to be interfering with the second macro.
I'm getting following error using rust nightly:
error[E0658]: `if` is not allowed in a `const fn`
--> /home/pranjalssh/.cargo/registry/src/github.com-1ecc6299db9ec823/field-offset-0.3.2/src/lib.rs:126:9
|
126 | assert!(offset + mem::size_of::<U>() <= mem::size_of::<T>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #49146 <https://github.com/rust-lang/rust/issues/49146> for more information
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.
error: could not compile `field-offset`.
I'm getting this in rust-nightly CI
Compiling field-offset v0.3.3
error[E0557]: feature has been removed
--> /github/home/.cargo/registry/src/github.com-1ecc6299db9ec823/field-offset-0.3.3/src/lib.rs:3:53
|
3 | #![cfg_attr(fieldoffset_assert_in_const_fn, feature(const_fn))]
| ^^^^^^^^ feature has been removed
|
= note: split into finer-grained feature gates
I would expect this to work without the warning.
#[macro_use]
extern crate field_offset;
struct Foo {
bar: u8
}
pub unsafe fn f() {
offset_of!(Foo => bar);
}
warning: unnecessary `unsafe` block
--> src/main.rs:9:5
|
8 | pub unsafe fn f() {
| ----------------- because it's nested under this `unsafe` fn
9 | offset_of!(Foo => bar);
| ^^^^^^^^^^^^^^^^^^^^^^^ unnecessary `unsafe` block
|
= note: #[warn(unused_unsafe)] on by default
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.