Semántico sayk'uy · 11
bindings/kotlin/stretch/src/main/rust/src/lib.rs
diff --git a/bindings/kotlin/stretch/src/main/rust/src/lib.rs b/bindings/kotlin/stretch/src/main/rust/src/lib.rs
index 04a32c925..69aa6f6be 100644
--- a/bindings/kotlin/stretch/src/main/rust/src/lib.rs
+++ b/bindings/kotlin/stretch/src/main/rust/src/lib.rs
@@ -1,15 +1,15 @@
#![cfg(target_os = "android")]
#![allow(non_snake_case)]
-use std::f32;
use jni::objects::{JObject, JValue};
-use jni::sys::{jboolean, jint, jfloat, jlong, jlongArray, jfloatArray};
+use jni::sys::{jboolean, jfloat, jfloatArray, jint, jlong, jlongArray};
use jni::JNIEnv;
-use stretch::node::*;
-use stretch::style::*;
-use stretch::result::{Layout};
+use std::f32;
use stretch::geometry::*;
+use stretch::node::*;
use stretch::number::*;
+use stretch::result::Layout;
+use stretch::style::*;
fn dimension(t: jint, v: jfloat) -> Dimension {
match t {
@@ -94,26 +94,26 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
maxHeightType: jint,
maxHeightValue: jfloat,
- aspectRatio: jfloat
+ aspectRatio: jfloat,
) -> jlong {
let style = Style {
display: match display {
0 => Display::Flex,
1 => Display::None,
- _ => panic!()
+ _ => panic!(),
},
position_type: match positionType {
0 => PositionType::Relative,
1 => PositionType::Absolute,
- _ => panic!()
+ _ => panic!(),
},
direction: match direction {
0 => Direction::Inherit,
1 => Direction::LTR,
2 => Direction::RTL,
- _ => panic!()
+ _ => panic!(),
},
flex_direction: match flexDirection {
@@ -121,21 +121,21 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
1 => FlexDirection::Column,
2 => FlexDirection::RowReverse,
3 => FlexDirection::ColumnReverse,
- _ => panic!()
+ _ => panic!(),
},
flex_wrap: match flexWrap {
0 => FlexWrap::NoWrap,
1 => FlexWrap::Wrap,
2 => FlexWrap::WrapReverse,
- _ => panic!()
+ _ => panic!(),
},
overflow: match overflow {
0 => Overflow::Visible,
1 => Overflow::Hidden,
2 => Overflow::Scroll,
- _ => panic!()
+ _ => panic!(),
},
align_items: match alignItems {
@@ -144,7 +144,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
2 => AlignItems::Center,
3 => AlignItems::Baseline,
4 => AlignItems::Stretch,
- _ => panic!()
+ _ => panic!(),
},
align_self: match alignSelf {
@@ -154,7 +154,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
3 => AlignSelf::Center,
4 => AlignSelf::Baseline,
5 => AlignSelf::Stretch,
- _ => panic!()
+ _ => panic!(),
},
align_content: match alignContent {
@@ -164,7 +164,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
3 => AlignContent::Stretch,
4 => AlignContent::SpaceBetween,
5 => AlignContent::SpaceAround,
- _ => panic!()
+ _ => panic!(),
},
justify_content: match justifyContent {
@@ -174,7 +174,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
3 => JustifyContent::SpaceBetween,
4 => JustifyContent::SpaceAround,
5 => JustifyContent::SpaceEvenly,
- _ => panic!()
+ _ => panic!(),
},
position: Rect {
@@ -210,10 +210,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
flex_basis: dimension(flexBasisType, flexBasisValue),
- size: Size {
- width: dimension(widthType, widthValue),
- height: dimension(heightType, heightValue),
- },
+ size: Size { width: dimension(widthType, widthValue), height: dimension(heightType, heightValue) },
min_size: Size {
width: dimension(minWidthType, minWidthValue),
@@ -225,22 +222,14 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Style_nConstruct(
height: dimension(maxHeightType, maxHeightValue),
},
- aspect_ratio: if f32::is_nan(aspectRatio) {
- Number::Undefined
- } else {
- Number::Defined(aspectRatio)
- },
+ aspect_ratio: if f32::is_nan(aspectRatio) { Number::Undefined } else { Number::Defined(aspectRatio) },
};
Box::into_raw(Box::new(style)) as jlong
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Style_nFree(
- _: JNIEnv,
- _: JObject,
- style: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Style_nFree(_: JNIEnv, _: JObject, style: jlong) {
let _style: Box<Style> = Box::from_raw(style as *mut Style);
}
@@ -255,9 +244,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nConstruct(
let mut buff = vec![0; num_children as usize];
env.get_long_array_region(children, 0, &mut buff).unwrap();
- let children = buff.into_iter().map(|ptr| {
- &*Box::leak(Box::from_raw(ptr as *mut Node))
- }).collect();
+ let children = buff.into_iter().map(|ptr| &*Box::leak(Box::from_raw(ptr as *mut Node))).collect();
let style: Box<Style> = Box::from_raw(style as *mut Style);
let node = Node::new(*Box::leak(style), children);
@@ -274,33 +261,32 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nConstructLeaf(
) -> jlong {
let measure = env.new_global_ref(measure).unwrap();
let style: Box<Style> = Box::from_raw(style as *mut Style);
- let node = Node::new_leaf(*Box::leak(style), Box::new(move |constraint| {
- let result = env.call_method(
- measure.as_obj(),
- "measure",
- "(FF)[F",
- &[JValue::from(constraint.width.or_else(f32::NAN)), JValue::from(constraint.height.or_else(f32::NAN))],
- );
-
- match result {
- Ok(result) => {
- let size = result.l().unwrap().into_inner() as jfloatArray;
- let mut buff: [f32; 2] = [0.0, 0.0];
- env.get_float_array_region(size, 0, &mut buff).unwrap();
- Ok(Size { width: buff[0], height: buff[1] })
+ let node = Node::new_leaf(
+ *Box::leak(style),
+ Box::new(move |constraint| {
+ let result = env.call_method(
+ measure.as_obj(),
+ "measure",
+ "(FF)[F",
+ &[JValue::from(constraint.width.or_else(f32::NAN)), JValue::from(constraint.height.or_else(f32::NAN))],
+ );
+
+ match result {
+ Ok(result) => {
+ let size = result.l().unwrap().into_inner() as jfloatArray;
+ let mut buff: [f32; 2] = [0.0, 0.0];
+ env.get_float_array_region(size, 0, &mut buff).unwrap();
+ Ok(Size { width: buff[0], height: buff[1] })
+ }
+ Err(err) => Err(Box::new(err)),
}
- Err(err) => Err(Box::new(err)),
- }
- }));
+ }),
+ );
Box::into_raw(Box::new(node)) as jlong
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nFree(
- _: JNIEnv,
- _: JObject,
- node: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nFree(_: JNIEnv, _: JObject, node: jlong) {
let _node: Box<Node> = Box::from_raw(node as *mut Node);
}
@@ -315,32 +301,27 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nSetMeasure(
let node: Box<Node> = Box::from_raw(node as *mut Node);
Box::leak(node).set_measure(Some(Box::new(move |constraint| {
- let result = env.call_method(
- measure.as_obj(),
- "measure",
- "(FF)[F",
- &[JValue::from(constraint.width.or_else(f32::NAN)), JValue::from(constraint.height.or_else(f32::NAN))],
- );
-
- match result {
- Ok(result) => {
- let size = result.l().unwrap().into_inner() as jfloatArray;
- let mut buff: [f32; 2] = [0.0, 0.0];
- env.get_float_array_region(size, 0, &mut buff).unwrap();
- Ok(Size { width: buff[0], height: buff[1] })
- }
- Err(err) => Err(Box::new(err)),
- }
- })));
+ let result = env.call_method(
+ measure.as_obj(),
+ "measure",
+ "(FF)[F",
+ &[JValue::from(constraint.width.or_else(f32::NAN)), JValue::from(constraint.height.or_else(f32::NAN))],
+ );
+
+ match result {
+ Ok(result) => {
+ let size = result.l().unwrap().into_inner() as jfloatArray;
+ let mut buff: [f32; 2] = [0.0, 0.0];
+ env.get_float_array_region(size, 0, &mut buff).unwrap();
+ Ok(Size { width: buff[0], height: buff[1] })
+ }
+ Err(err) => Err(Box::new(err)),
+ }
+ })));
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nSetStyle(
- _: JNIEnv,
- _: JObject,
- node: jlong,
- style: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nSetStyle(_: JNIEnv, _: JObject, node: jlong, style: jlong) {
let style: Box<Style> = Box::from_raw(style as *mut Style);
let mut node: Box<Node> = Box::from_raw(node as *mut Node);
node.set_style(*style);
@@ -350,21 +331,13 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nSetStyle(
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nIsDirty(
- _: JNIEnv,
- _: JObject,
- node: jlong,
-) -> jboolean {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nIsDirty(_: JNIEnv, _: JObject, node: jlong) -> jboolean {
let node: Box<Node> = Box::from_raw(node as *mut Node);
Box::leak(node).dirty() as jboolean
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nMarkDirty(
- _: JNIEnv,
- _: JObject,
- node: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nMarkDirty(_: JNIEnv, _: JObject, node: jlong) {
let node: Box<Node> = Box::from_raw(node as *mut Node);
Box::leak(node).mark_dirty();
}
@@ -380,21 +353,14 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nSetChildren(
let mut buff = vec![0; num_children as usize];
env.get_long_array_region(children, 0, &mut buff).unwrap();
- let children = buff.into_iter().map(|ptr| {
- &*Box::leak(Box::from_raw(ptr as *mut Node))
- }).collect();
+ let children = buff.into_iter().map(|ptr| &*Box::leak(Box::from_raw(ptr as *mut Node))).collect();
let node: Box<Node> = Box::from_raw(node as *mut Node);
Box::leak(node).set_children(children);
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nAddChild(
- _: JNIEnv,
- _: JObject,
- node: jlong,
- child: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nAddChild(_: JNIEnv, _: JObject, node: jlong, child: jlong) {
let mut node: Box<Node> = Box::from_raw(node as *mut Node);
let child: Box<Node> = Box::from_raw(child as *mut Node);
node.add_child(&*child);
@@ -420,12 +386,7 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nReplaceChildAtIndex(
}
#[no_mangle]
-pub unsafe extern "C" fn Java_app_visly_stretch_Node_nRemoveChild(
- _: JNIEnv,
- _: JObject,
- node: jlong,
- child: jlong,
-) {
+pub unsafe extern "C" fn Java_app_visly_stretch_Node_nRemoveChild(_: JNIEnv, _: JObject, node: jlong, child: jlong) {
let mut node: Box<Node> = Box::from_raw(node as *mut Node);
let child: Box<Node> = Box::from_raw(child as *mut Node);
node.remove_child(&*child);
@@ -454,18 +415,12 @@ pub unsafe extern "C" fn Java_app_visly_stretch_Node_nComputeLayout(
height: jfloat,
) -> jfloatArray {
let node: Box<Node> = Box::from_raw(node as *mut Node);
- let layout = Box::leak(node).compute_layout(Size {
- width: if f32::is_nan(width) {
- Number::Undefined
- } else {
- Number::Defined(width)
- },
- height: if f32::is_nan(height) {
- Number::Undefined
- } else {
- Number::Defined(height)
- },
- }).unwrap();
+ let layout = Box::leak(node)
+ .compute_layout(Size {
+ width: if f32::is_nan(width) { Number::Undefined } else { Number::Defined(width) },
+ height: if f32::is_nan(height) { Number::Undefined } else { Number::Defined(height) },
+ })
+ .unwrap();
let mut output = vec![];
copy_output(&layout, &mut output);