Skip to content

Commit 1ef728b

Browse files
ci(format): Action to check formatting
This action checks the formatting of the rust files Signed-off-by: Vaishnav Sabari Girish <vaishnav.sabari.girish@gmail.com>
1 parent 9257477 commit 1ef728b

6 files changed

Lines changed: 71 additions & 65 deletions

File tree

.githooks/pre-commit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
set -e
3+
4+
REPO_ROOT=$(git rev-parse --show-toplevel)
5+
6+
cargo fmt --all
7+
8+
git add -u

.github/workflows/fmt.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Format the Rust Code
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ "main" ]
7+
8+
jobs:
9+
fmt:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v6
14+
- uses: dtolnay/rust-toolchain@stable
15+
with:
16+
components: rustfmt
17+
- run: cargo fmt --all -- --check

crates/ns_io/src/input.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use std::ffi::{c_double, c_float, c_int};
12
use std::io::{self};
2-
use std::ffi::{c_int, c_float,c_double};
33

44
// Helper function to read a line from stdin safely
55
fn read_line_buffer() -> String {
@@ -12,7 +12,7 @@ fn read_line_buffer() -> String {
1212

1313
// Read Integer
1414
#[unsafe(no_mangle)]
15-
/// # Safety
15+
/// # Safety
1616
///
1717
/// This function reads an integer input
1818
pub unsafe extern "C" fn ns_read_int(ptr: *mut c_int) {
@@ -25,17 +25,17 @@ pub unsafe extern "C" fn ns_read_int(ptr: *mut c_int) {
2525
// Try to parse. If it fails, default to 0
2626
let val: c_int = input.parse().unwrap_or(0);
2727

28-
// Unsafe: Write value to C memory address
28+
// Unsafe: Write value to C memory address
2929
unsafe {
3030
*ptr = val;
3131
}
3232
}
3333

34-
// Read float
34+
// Read float
3535
#[unsafe(no_mangle)]
36-
/// # Safety
36+
/// # Safety
3737
///
38-
/// This function reads a floating point input
38+
/// This function reads a floating point input
3939
pub unsafe extern "C" fn ns_read_float(ptr: *mut c_float) {
4040
if ptr.is_null() {
4141
return;
@@ -46,17 +46,17 @@ pub unsafe extern "C" fn ns_read_float(ptr: *mut c_float) {
4646
// Try to parse. If it fails, default to 0
4747
let val: c_float = input.parse().unwrap_or(0.0);
4848

49-
// Unsafe: Write value to C memory address
49+
// Unsafe: Write value to C memory address
5050
unsafe {
5151
*ptr = val;
5252
}
5353
}
5454

55-
// Read double
55+
// Read double
5656
#[unsafe(no_mangle)]
57-
/// # Safety
57+
/// # Safety
5858
///
59-
/// This function reads a double input
59+
/// This function reads a double input
6060
pub unsafe extern "C" fn ns_read_double(ptr: *mut c_double) {
6161
if ptr.is_null() {
6262
return;
@@ -67,9 +67,8 @@ pub unsafe extern "C" fn ns_read_double(ptr: *mut c_double) {
6767
// Try to parse. If it fails, default to 0
6868
let val: c_double = input.parse().unwrap_or(0.0);
6969

70-
// Unsafe: Write value to C memory address
70+
// Unsafe: Write value to C memory address
7171
unsafe {
7272
*ptr = val;
7373
}
7474
}
75-

crates/ns_io/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Print Module
2-
pub mod print;
1+
// Print Module
32
pub mod input;
3+
pub mod print;

crates/ns_io/src/print.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1+
use ns_string::NsString;
12
use std::ffi::CStr;
2-
use std::os::raw::c_char;
33
use std::io::{self, Write};
4-
use ns_string::NsString;
4+
use std::os::raw::c_char;
55

66
// No Newline functions
77

8-
// Print Integer
8+
// Print Integer
99
#[unsafe(no_mangle)]
1010
pub extern "C" fn ns_print_int(val: i32) {
1111
// Adds newline by default
1212
print!("{}", val);
1313
io::stdout().flush().unwrap();
1414
}
1515

16-
// Print Float
16+
// Print Float
1717
#[unsafe(no_mangle)]
1818
pub extern "C" fn ns_print_float(val: f32) {
1919
// Adds newline by default
2020
print!("{}", val);
2121
io::stdout().flush().unwrap();
2222
}
2323

24-
// Print Double
24+
// Print Double
2525
#[unsafe(no_mangle)]
2626
pub extern "C" fn ns_print_double(val: f64) {
27-
// Adds newline by default
27+
// Adds newline by default
2828
print!("{}", val);
2929
io::stdout().flush().unwrap();
3030
}
3131

32-
3332
// Print String
3433
#[unsafe(no_mangle)]
3534
// If the C string is not null terminaltes '\0' , the function will keep on reading memory until
3635
// the program crashes (Segfault)
37-
// The below suppression is to prevent that
36+
// The below suppression is to prevent that
3837
#[allow(clippy::not_unsafe_ptr_arg_deref)]
3938
pub extern "C" fn ns_print_string(ptr: *const c_char) {
4039
// Never Dereference a NULL pointer
@@ -44,9 +43,7 @@ pub extern "C" fn ns_print_string(ptr: *const c_char) {
4443
return;
4544
}
4645

47-
let c_str = unsafe {
48-
CStr::from_ptr(ptr)
49-
};
46+
let c_str = unsafe { CStr::from_ptr(ptr) };
5047

5148
// Converting to Rust String
5249
// "to_string_lossy()" is best if the string has non-UTF-8 characters
@@ -56,33 +53,32 @@ pub extern "C" fn ns_print_string(ptr: *const c_char) {
5653
}
5754

5855
// Newline functions
59-
// Print Integer
56+
// Print Integer
6057
#[unsafe(no_mangle)]
6158
pub extern "C" fn ns_println_int(val: i32) {
6259
// Adds newline by default
6360
println!("{}", val);
6461
}
6562

66-
// Print Float
63+
// Print Float
6764
#[unsafe(no_mangle)]
6865
pub extern "C" fn ns_println_float(val: f32) {
6966
// Adds newline by default
7067
println!("{}", val);
7168
}
7269

73-
// Print Double
70+
// Print Double
7471
#[unsafe(no_mangle)]
7572
pub extern "C" fn ns_println_double(val: f64) {
76-
// Adds newline by default
73+
// Adds newline by default
7774
println!("{}", val);
7875
}
7976

80-
8177
// Print String
8278
#[unsafe(no_mangle)]
8379
// If the C string is not null terminaltes '\0' , the function will keep on reading memory until
8480
// the program crashes (Segfault)
85-
// The below suppression is to prevent that
81+
// The below suppression is to prevent that
8682
#[allow(clippy::not_unsafe_ptr_arg_deref)]
8783
pub extern "C" fn ns_println_string(ptr: *const c_char) {
8884
// Never Dereference a NULL pointer
@@ -91,9 +87,7 @@ pub extern "C" fn ns_println_string(ptr: *const c_char) {
9187
return;
9288
}
9389

94-
let c_str = unsafe {
95-
CStr::from_ptr(ptr)
96-
};
90+
let c_str = unsafe { CStr::from_ptr(ptr) };
9791

9892
// Converting to Rust String
9993
// "to_string_lossy()" is best if the string has non-UTF-8 characters
@@ -106,13 +100,9 @@ pub extern "C" fn ns_println_string(ptr: *const c_char) {
106100
#[unsafe(no_mangle)]
107101
pub extern "C" fn ns_print_ns_string(val: NsString) {
108102
let bytes = if val.is_heap {
109-
unsafe {
110-
std::slice::from_raw_parts(val.data.heap.ptr, val.len)
111-
}
103+
unsafe { std::slice::from_raw_parts(val.data.heap.ptr, val.len) }
112104
} else {
113-
unsafe {
114-
&val.data.inline_data[..val.len]
115-
}
105+
unsafe { &val.data.inline_data[..val.len] }
116106
};
117107

118108
// COnvert to rust string (To handle non-UTF-8)
@@ -123,17 +113,13 @@ pub extern "C" fn ns_print_ns_string(val: NsString) {
123113
io::stdout().flush().unwrap();
124114
}
125115

126-
// Newline
116+
// Newline
127117
#[unsafe(no_mangle)]
128118
pub extern "C" fn ns_println_ns_string(val: NsString) {
129119
let bytes = if val.is_heap {
130-
unsafe {
131-
std::slice::from_raw_parts(val.data.heap.ptr, val.len)
132-
}
120+
unsafe { std::slice::from_raw_parts(val.data.heap.ptr, val.len) }
133121
} else {
134-
unsafe {
135-
&val.data.inline_data[..val.len]
136-
}
122+
unsafe { &val.data.inline_data[..val.len] }
137123
};
138124

139125
// COnvert to rust string (To handle non-UTF-8)
@@ -142,4 +128,3 @@ pub extern "C" fn ns_println_ns_string(val: NsString) {
142128
// Print and flush
143129
println!("{}", rust_str);
144130
}
145-

crates/ns_string/src/lib.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
use std::ffi::CStr;
2-
use std::os::raw::c_char;
32
use std::mem::ManuallyDrop;
3+
use std::os::raw::c_char;
44

55
// Heap allocation
66
#[repr(C)]
77
pub struct NsStringHeap {
88
pub ptr: *mut u8,
9-
pub capacity: usize
9+
pub capacity: usize,
1010
}
1111

1212
// Union: 24 bytes of inline chars or the heap struct
1313
#[repr(C)]
1414
pub union NsStringData {
1515
pub inline_data: [u8; 24],
16-
pub heap: ManuallyDrop<NsStringHeap>,
16+
pub heap: ManuallyDrop<NsStringHeap>,
1717
}
1818

1919
// Final string struct
2020
#[repr(C)]
2121
pub struct NsString {
2222
pub len: usize,
2323
pub is_heap: bool,
24-
pub data: NsStringData
24+
pub data: NsStringData,
2525
}
2626

2727
#[allow(clippy::not_unsafe_ptr_arg_deref)]
@@ -31,13 +31,13 @@ pub extern "C" fn ns_string_new(c_str: *const c_char) -> NsString {
3131
return NsString {
3232
len: 0,
3333
is_heap: false,
34-
data: NsStringData { inline_data: [0; 24] }
34+
data: NsStringData {
35+
inline_data: [0; 24],
36+
},
3537
};
3638
}
3739

38-
let rust_str = unsafe {
39-
CStr::from_ptr(c_str)
40-
}.to_bytes();
40+
let rust_str = unsafe { CStr::from_ptr(c_str) }.to_bytes();
4141

4242
let len = rust_str.len();
4343

@@ -49,7 +49,9 @@ pub extern "C" fn ns_string_new(c_str: *const c_char) -> NsString {
4949
NsString {
5050
len,
5151
is_heap: false,
52-
data: NsStringData { inline_data: inline }
52+
data: NsStringData {
53+
inline_data: inline,
54+
},
5355
}
5456
} else {
5557
// If it is more than 24 bytes, put it on the heap
@@ -67,11 +69,8 @@ pub extern "C" fn ns_string_new(c_str: *const c_char) -> NsString {
6769
NsString {
6870
len,
6971
is_heap: true,
70-
data: NsStringData {
71-
heap: ManuallyDrop::new(NsStringHeap {
72-
ptr,
73-
capacity
74-
})
72+
data: NsStringData {
73+
heap: ManuallyDrop::new(NsStringHeap { ptr, capacity }),
7574
},
7675
}
7776
}
@@ -84,9 +83,7 @@ pub extern "C" fn ns_string_free(s: *mut NsString) {
8483
return;
8584
}
8685

87-
let s_ref = unsafe {
88-
&mut *s
89-
};
86+
let s_ref = unsafe { &mut *s };
9087

9188
if s_ref.is_heap {
9289
unsafe {

0 commit comments

Comments
 (0)