Introduce safer layer of sugar for nsStyleUnion#12521
Introduce safer layer of sugar for nsStyleUnion#12521bors-servo merged 3 commits intoservo:masterfrom
Conversation
|
Heads up! This PR modifies the following files:
|
|
Oh, another issue is that we need to make all unit/union fields private and add unsafe accessors. This will need to be done through bindgen changes, which I'll need to look into. For now, we're directly setting the mUnit/mValue fields only for z-index, and there's a justifying comment. |
| *(*self.union).mInt.as_mut() = i as i32; | ||
| } | ||
| Calc(calc) => { | ||
| *self.unit = eStyleUnit_Calc; |
There was a problem hiding this comment.
This line shouldn't be here, since we assert otherwise in set_calc_value.
303e9af to
afd05df
Compare
|
I went ahead and made the traits changes. They're in a separate commit so that we can still pick the old way of doing it if we want to. It might be possible to clean up some of the impls in that file with macros. Not sure if it's necessary though. |
|
rust-lang/rust-bindgen#20 will make it possible to make this even more safe 😀 |
|
So much nicer! r=me with those comments addressed, but please smoketest this a bit. :-) @bors-servo delegate+ Reviewed 1 of 1 files at r1, 1 of 4 files at r2, 3 of 3 files at r3. components/style/properties/gecko.mako.rs, line 371 [r3] (raw file):
Don't you mean height here? components/style/properties/gecko.mako.rs, line 377 [r3] (raw file):
And don't you want y_index here? ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs, line 188 [r2] (raw file):
|
|
✌️ @Manishearth can now approve this pull request |
|
Review status: 2 of 6 files reviewed at latest revision, 11 unresolved discussions. components/style/properties/gecko.mako.rs, line 371 [r3] (raw file):
|
|
@bors-servo r=bholley I didn't address the |
|
📌 Commit 73d7db0 has been approved by |
|
⌛ Testing commit 73d7db0 with merge 4a77cbd... |
Introduce safer layer of sugar for nsStyleUnion This routes (almost) all access to nsStyleUnion through a largely safe interface, CoordData. It also introduces a corresponding Rust enum, CoordDataValue, which can be used when interacting with CoordData LLVM should optimize the costs away in release mode. eddyb tested this a bit, and LLVM has no trouble threading matches together with inlining -- so all of the matches using enums here will have the same generated code as the old matches on the units. Some unresolved questions: Should I provide convenience methods like `set_coord`, `set_auto`, etc on CoordData? `.set_enum(CoordDataValue::Something)` should optimize down to the same thing, but the convenience methods look cleaner and won't load the optimizer as much. Also, we're converting immutable references to mutable ones, which can be used to cause unsafety using some acrobatics. Perhaps a trait-based approach is better? The issue is that in some places we only have a `&CoordData` (eg copy_from), but CoordData internally is `*mut`. It would be nice if CoordData could parametrize over its mutability, but Rust doesn't let us do that. The alternate approach is to make CoordData a trait (also CoordDataMut). The trait requires you to implement `get_union()` and `get_unit()`, and gives you the rest of the functions for free. `nsStyleCoord` would directly implement both traits. `nsStyleSides` would have `data_at(idx)` and `data_at_mut(idx)` methods which return a struct `SidesData` containing a reference to the Sides and the index, which itself implements the `CoordData` and `CoordDataMut` trait (we need two traits here because there will have to be two `SidesData` structs). I decided not to implement the traits approach first since it's pretty trivial to change this code to use traits, and the current design is more straightforward. Thoughts? r? @bholley cc @emilio @heycam <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12521) <!-- Reviewable:end -->
|
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel, mac-dev-unit, mac-rel-css, mac-rel-wpt, windows-dev |
Add support for 'unsafe fields' Needed for the next step of servo/servo#12521 These are fields which are private but have unsafe accessor functions. Since we generate bindings in a separate module, we can't touch private fields from other parts of the module (an alternative is to inject a footer with these private impls). `pub(restricted)` exists, but is not stable. r? @emilio
This routes (almost) all access to nsStyleUnion through a largely safe interface, CoordData.
It also introduces a corresponding Rust enum, CoordDataValue, which can be used when interacting with CoordData
LLVM should optimize the costs away in release mode. eddyb tested this a bit, and LLVM has no trouble threading matches together with inlining -- so all of the matches using enums here will have the same generated code as the old matches on the units.
Some unresolved questions:
Should I provide convenience methods like
set_coord,set_auto, etc on CoordData?.set_enum(CoordDataValue::Something)should optimize down to the same thing, but the convenience methods look cleaner and won't load the optimizer as much.Also, we're converting immutable references to mutable ones, which can be used to cause unsafety using some acrobatics. Perhaps a trait-based approach is better?
The issue is that in some places we only have a
&CoordData(eg copy_from), but CoordData internally is*mut. It would be nice if CoordData could parametrize over its mutability, but Rust doesn't let us do that.The alternate approach is to make CoordData a trait (also CoordDataMut). The trait requires you to implement
get_union()andget_unit(), and gives you the rest of the functions for free.nsStyleCoordwould directly implement both traits.nsStyleSideswould havedata_at(idx)anddata_at_mut(idx)methods which return a structSidesDatacontaining a reference to the Sides and the index, which itself implements theCoordDataandCoordDataMuttrait (we need two traits here because there will have to be twoSidesDatastructs).I decided not to implement the traits approach first since it's pretty trivial to change this code to use traits, and the current design is more straightforward.
Thoughts?
r? @bholley
cc @emilio @heycam
This change is