Skip to content

Commit 8781bb4

Browse files
committed
refactor: add proper unsafe sign
1 parent 1a4dbc9 commit 8781bb4

File tree

7 files changed

+243
-261
lines changed

7 files changed

+243
-261
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Major crates:
1313
| ---------- | ----------- | ---- |
1414
| [float-pigment-css](https://crates.io/crates/float-pigment-css) | CSS parser. | [![docs.rs](https://img.shields.io/docsrs/float-pigment-css?style=flat-square)](https://docs.rs/float-pigment-css) |
1515
| [float-pigment-layout](https://crates.io/crates/float-pigment-layout) | Layout engine which supports common CSS `display`, including `flex` `block` and `inline`. | [![docs.rs](https://img.shields.io/docsrs/float-pigment-layout?style=flat-square)](https://docs.rs/float-pigment-layout) |
16-
| [float-pigment-forest](https://crates.io/crates/float-pigment-forest) | Tree implementation that works with `float-pigment-layout`. | [![docs.rs](https://img.shields.io/docsrs/float-pigment-forest?style=flat-square)](https://docs.rs/float-pigment-forest) |
16+
| [float-pigment-forest](https://crates.io/crates/float-pigment-forest) | Tree implementation that works with `float-pigment-layout` for C++ bondings. | [![docs.rs](https://img.shields.io/docsrs/float-pigment-forest?style=flat-square)](https://docs.rs/float-pigment-forest) |
1717
| [float-pigment](https://crates.io/crates/float-pigment) | The collection of all crates above, with C++ bindings. | [![docs.rs](https://img.shields.io/docsrs/float-pigment?style=flat-square)](https://docs.rs/float-pigment) |
1818

1919

@@ -57,9 +57,9 @@ Supported `position`:
5757

5858
float-pigment-layout requires an external node tree implementation.
5959

60-
Usually the node tree should be implemented in high-level code or some other dedicated modules, but that is not always this case.
60+
Usually the node tree should be implemented in high-level code or some other dedicated modules.
6161

62-
If you do not implement a node tree yourself, or the tree implementation is not in rust, this crate can help.
62+
However, if the main implementation is in C++, this crate can help you builds a tree in rust.
6363

6464

6565
## LICENSE

float-pigment-forest/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ harness = false
3333

3434
[features]
3535
default = []
36-
build-cpp-header = ["cbindgen"]
36+
build-cpp-header = ["cbindgen", "fs_extra"]
3737

3838
[dependencies]
3939
float-pigment-css = { workspace = true }
@@ -45,6 +45,7 @@ lru = "0.7.8"
4545
float-pigment-forest-macro = { workspace = true }
4646
bit-vec = "0.6.3"
4747
lazy_static = "1.4"
48+
fs_extra = { version = "1.3", optional = true }
4849

4950
[dev-dependencies]
5051
rand = "0.8.5"
@@ -55,5 +56,5 @@ rustc-hash = "1.1.0"
5556
[target.'cfg(target_os="macos")'.dev-dependencies]
5657
piston_window = "0.128.0"
5758

58-
[lints]
59-
workspace = true
59+
[lints.clippy]
60+
missing_safety_doc = "allow"

float-pigment-forest/benches/layout.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ fn gen_tree() -> &'static Node {
2424
}
2525

2626
fn layout_test(root: &Node) {
27-
root.layout(
28-
OptionSize::new(
29-
OptionNum::some(30000.0.to_fixed()),
30-
OptionNum::some(800.0.to_fixed()),
31-
),
32-
Size::new(375.0.to_fixed(), 800.0.to_fixed()),
33-
);
27+
unsafe {
28+
root.layout(
29+
OptionSize::new(
30+
OptionNum::some(30000.0.to_fixed()),
31+
OptionNum::some(800.0.to_fixed()),
32+
),
33+
Size::new(375.0.to_fixed(), 800.0.to_fixed()),
34+
);
35+
}
3436
}
3537

3638
fn criterion_benchmark(c: &mut Criterion) {

float-pigment-forest/src/layout/layout_impl.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl LayoutTreeNode for Node {
101101
}
102102
}
103103
if !skip_measure {
104-
if let Some(func) = self.measure_func() {
104+
if let Some(func) = unsafe { self.measure_func() } {
105105
let mut width_measure_mode = MeasureMode::AtMost;
106106
let mut height_measure_mode = MeasureMode::AtMost;
107107
let (min_width, max_width) = if let Some(req_size_width) = req_size.width.val() {
@@ -135,7 +135,7 @@ impl LayoutTreeNode for Node {
135135
};
136136
let mut size_from_cache = false;
137137
if self.node_type() == NodeType::Text {
138-
if let Some(cache) = self.measure_cache().as_mut() {
138+
if let Some(cache) = unsafe { self.measure_cache() }.as_mut() {
139139
if let Some(size_cache) = cache.get(&(
140140
OptionSize::new(
141141
OptionNum::some(min_width).to_hashable(),
@@ -183,7 +183,7 @@ impl LayoutTreeNode for Node {
183183
measure_size.height.clamp(min.height, max.height),
184184
);
185185
if self.node_type() == NodeType::Text {
186-
if let Some(cache) = self.measure_cache().as_mut() {
186+
if let Some(cache) = unsafe { self.measure_cache() }.as_mut() {
187187
cache.put(
188188
(
189189
OptionSize::new(
@@ -209,18 +209,18 @@ impl LayoutTreeNode for Node {
209209
let mut baseline = size.to_vector();
210210
let mut baseline_from_cache = false;
211211
if self.node_type() == NodeType::Text {
212-
if let Some(cache) = self.baseline_cache().as_mut() {
212+
if let Some(cache) = unsafe { self.baseline_cache() }.as_mut() {
213213
if let Some(baseline_cache) = cache.get(&Size::new(size.width, size.height)) {
214214
baseline_from_cache = true;
215215
baseline = Vector::new(Len::zero(), *baseline_cache);
216216
}
217217
}
218218
}
219219
if !baseline_from_cache {
220-
if let Some(func) = self.baseline_func() {
220+
if let Some(func) = unsafe { self.baseline_func() } {
221221
let ret = func(convert_node_ref_to_ptr(self), size.width, size.height);
222222
baseline = Vector::new(Len::zero(), ret);
223-
if let Some(cache) = self.baseline_cache().as_mut() {
223+
if let Some(cache) = unsafe { self.baseline_cache() }.as_mut() {
224224
cache.put(Size::new(size.width, size.height), ret);
225225
}
226226
}
@@ -262,7 +262,7 @@ impl LayoutTreeNode for Node {
262262
impl LayoutTreeVisitor<Node> for Node {
263263
#[inline]
264264
fn parent(&self) -> Option<&Node> {
265-
Node::parent(self)
265+
unsafe { Node::parent(self) }
266266
}
267267

268268
#[inline]
@@ -271,7 +271,7 @@ impl LayoutTreeVisitor<Node> for Node {
271271
F: FnMut(&'a Node, usize),
272272
Node: 'a,
273273
{
274-
self.for_each_child_node(f)
274+
unsafe { self.for_each_child_node(f) }
275275
}
276276

277277
#[inline]
@@ -281,7 +281,7 @@ impl LayoutTreeVisitor<Node> for Node {
281281

282282
#[inline]
283283
fn child_at(&self, index: usize) -> Option<&Node> {
284-
self.get_child_at(index)
284+
unsafe { self.get_child_at(index) }
285285
}
286286
}
287287
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)