Move func "constraints_snap_distance" to Rust

This commit is contained in:
Alex Kotov 2022-09-07 18:50:29 +04:00
parent f8198f75ec
commit c77e47796c
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 27 additions and 9 deletions

View file

@ -2,8 +2,6 @@
#define MIN_MASTER_AREA_FACTOR 0.05
#define MAX_MASTER_AREA_FACTOR 0.95
#define MIN_SNAP_DISTANCE 1
#define MAX_SNAP_DISTANCE 10000
float constraints_default_master_area_factor(const float default_master_area_factor)
{
@ -16,10 +14,3 @@ float constraints_master_area_factor(const float master_area_factor)
if (master_area_factor > MAX_MASTER_AREA_FACTOR) return MAX_MASTER_AREA_FACTOR;
return master_area_factor;
}
unsigned int constraints_snap_distance(const unsigned int snap_distance)
{
if (snap_distance < MIN_SNAP_DISTANCE) return MIN_SNAP_DISTANCE;
if (snap_distance > MAX_SNAP_DISTANCE) return MAX_SNAP_DISTANCE;
return snap_distance;
}

View file

@ -8,6 +8,8 @@ const MIN_GAP_SIZE: c_int = 0;
const MAX_GAP_SIZE: c_int = 10000;
const MIN_MAX_CLIENTS_IN_MASTER: c_int = 1;
const MAX_MAX_CLIENTS_IN_MASTER: c_int = 10000;
const MIN_SNAP_DISTANCE: c_uint = 1;
const MAX_SNAP_DISTANCE: c_uint = 10000;
pub fn border_width(border_width: c_int) -> c_int {
if border_width < MIN_BORDER_WIDTH { return MIN_BORDER_WIDTH }
@ -41,6 +43,12 @@ pub fn max_clients_in_master(max_clients_in_master: c_int) -> c_int {
max_clients_in_master
}
pub fn snap_distance(snap_distance: c_uint) -> c_uint {
if snap_distance < MIN_SNAP_DISTANCE { return MIN_SNAP_DISTANCE }
if snap_distance > MAX_SNAP_DISTANCE { return MAX_SNAP_DISTANCE }
snap_distance
}
#[cfg(test)]
mod tests {
use super::*;
@ -100,4 +108,18 @@ mod tests {
assert_eq!(max_clients_in_master(10_001), 10_000);
assert_eq!(max_clients_in_master(20_000), 10_000);
}
#[test]
fn test_snap_distance() {
assert_eq!(snap_distance(0), 1);
assert_eq!(snap_distance(1), 1);
assert_eq!(snap_distance(2), 2);
assert_eq!(snap_distance(100), 100);
assert_eq!(snap_distance(9999), 9999);
assert_eq!(snap_distance(10_000), 10_000);
assert_eq!(snap_distance(10_001), 10_000);
assert_eq!(snap_distance(20_000), 10_000);
}
}

View file

@ -21,3 +21,8 @@ extern "C" fn constraints_gap_size(gap_size: c_int) -> c_int {
extern "C" fn constraints_max_clients_in_master(max_clients_in_master: c_int) -> c_int {
constraints::max_clients_in_master(max_clients_in_master)
}
#[no_mangle]
extern "C" fn constraints_snap_distance(snap_distance: c_uint) -> c_uint {
constraints::snap_distance(snap_distance)
}