Skip to content

Commit

Permalink
refactor: add proper unsafe sign
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Dec 4, 2024
1 parent 1a4dbc9 commit 8781bb4
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 261 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Major crates:
| ---------- | ----------- | ---- |
| [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) |
| [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) |
| [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) |
| [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) |
| [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) |


Expand Down Expand Up @@ -57,9 +57,9 @@ Supported `position`:

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

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

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


## LICENSE
Expand Down
7 changes: 4 additions & 3 deletions float-pigment-forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ harness = false

[features]
default = []
build-cpp-header = ["cbindgen"]
build-cpp-header = ["cbindgen", "fs_extra"]

[dependencies]
float-pigment-css = { workspace = true }
Expand All @@ -45,6 +45,7 @@ lru = "0.7.8"
float-pigment-forest-macro = { workspace = true }
bit-vec = "0.6.3"
lazy_static = "1.4"
fs_extra = { version = "1.3", optional = true }

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

[lints]
workspace = true
[lints.clippy]
missing_safety_doc = "allow"
16 changes: 9 additions & 7 deletions float-pigment-forest/benches/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ fn gen_tree() -> &'static Node {
}

fn layout_test(root: &Node) {
root.layout(
OptionSize::new(
OptionNum::some(30000.0.to_fixed()),
OptionNum::some(800.0.to_fixed()),
),
Size::new(375.0.to_fixed(), 800.0.to_fixed()),
);
unsafe {
root.layout(
OptionSize::new(
OptionNum::some(30000.0.to_fixed()),
OptionNum::some(800.0.to_fixed()),
),
Size::new(375.0.to_fixed(), 800.0.to_fixed()),
);
}
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
18 changes: 9 additions & 9 deletions float-pigment-forest/src/layout/layout_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl LayoutTreeNode for Node {
}
}
if !skip_measure {
if let Some(func) = self.measure_func() {
if let Some(func) = unsafe { self.measure_func() } {
let mut width_measure_mode = MeasureMode::AtMost;
let mut height_measure_mode = MeasureMode::AtMost;
let (min_width, max_width) = if let Some(req_size_width) = req_size.width.val() {
Expand Down Expand Up @@ -135,7 +135,7 @@ impl LayoutTreeNode for Node {
};
let mut size_from_cache = false;
if self.node_type() == NodeType::Text {
if let Some(cache) = self.measure_cache().as_mut() {
if let Some(cache) = unsafe { self.measure_cache() }.as_mut() {
if let Some(size_cache) = cache.get(&(
OptionSize::new(
OptionNum::some(min_width).to_hashable(),
Expand Down Expand Up @@ -183,7 +183,7 @@ impl LayoutTreeNode for Node {
measure_size.height.clamp(min.height, max.height),
);
if self.node_type() == NodeType::Text {
if let Some(cache) = self.measure_cache().as_mut() {
if let Some(cache) = unsafe { self.measure_cache() }.as_mut() {
cache.put(
(
OptionSize::new(
Expand All @@ -209,18 +209,18 @@ impl LayoutTreeNode for Node {
let mut baseline = size.to_vector();
let mut baseline_from_cache = false;
if self.node_type() == NodeType::Text {
if let Some(cache) = self.baseline_cache().as_mut() {
if let Some(cache) = unsafe { self.baseline_cache() }.as_mut() {
if let Some(baseline_cache) = cache.get(&Size::new(size.width, size.height)) {
baseline_from_cache = true;
baseline = Vector::new(Len::zero(), *baseline_cache);
}
}
}
if !baseline_from_cache {
if let Some(func) = self.baseline_func() {
if let Some(func) = unsafe { self.baseline_func() } {
let ret = func(convert_node_ref_to_ptr(self), size.width, size.height);
baseline = Vector::new(Len::zero(), ret);
if let Some(cache) = self.baseline_cache().as_mut() {
if let Some(cache) = unsafe { self.baseline_cache() }.as_mut() {
cache.put(Size::new(size.width, size.height), ret);
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ impl LayoutTreeNode for Node {
impl LayoutTreeVisitor<Node> for Node {
#[inline]
fn parent(&self) -> Option<&Node> {
Node::parent(self)
unsafe { Node::parent(self) }
}

#[inline]
Expand All @@ -271,7 +271,7 @@ impl LayoutTreeVisitor<Node> for Node {
F: FnMut(&'a Node, usize),
Node: 'a,
{
self.for_each_child_node(f)
unsafe { self.for_each_child_node(f) }
}

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

#[inline]
fn child_at(&self, index: usize) -> Option<&Node> {
self.get_child_at(index)
unsafe { self.get_child_at(index) }
}
}
#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit 8781bb4

Please sign in to comment.