Skip to content

Commit

Permalink
Merge pull request #30 from naomijub/drops-unused-pointers
Browse files Browse the repository at this point in the history
drops
  • Loading branch information
naomijub authored Apr 16, 2021
2 parents 67aba25 + 774ded1 commit 01893fe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brcode"
version = "1.4.2"
version = "1.4.3"
authors = ["Julia Naomi <[email protected]>"]
edition = "2018"
description = "Crate to parse and emit BR Codes"
Expand Down
19 changes: 19 additions & 0 deletions clj-brcode/test/clj_brcode/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@
(testing "biscuit is B8CE"
(is (= (crc16-ccitt "biscuit") "B8CE"))
(is (= (crc16-ccitt unchecked-code) "AD38"))))

(deftest drop
(testing "access edn after a drop"
(let [e edn
resp (edn->brcode e)]
(is (= resp code))
(is (= 1 (get e :payload-version)))))
(testing "access code after crc16"
(let [uc unchecked-code
crc (crc16-ccitt uc)
code (str uc crc)
expect (str unchecked-code "AD38")]
(is (= code expect)))
)
(testing "access code after drop"
(let [c code
edn (brcode->edn c)]
(is (= 1 (get edn :payload-version)))
(is (= (subs c 0 2) "00")))))
Binary file added libbrcode.dylib
Binary file not shown.
Binary file added libbrcode.so
Binary file not shown.
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ use std::str;

fn chars_to_string(pointer: *const c_char) -> String {
let slice = unsafe { CStr::from_ptr(pointer).to_bytes() };
drop(pointer);
str::from_utf8(slice).unwrap().to_string()
}

fn to_c_char(s: &str) -> *const c_char {
let cs = CString::new(s.as_bytes()).unwrap();
let ptr = cs.as_ptr();
mem::forget(cs);
drop(s);
ptr
}

#[no_mangle]
pub extern "C" fn crc16_ccitt_from_message(message: *const c_char) -> *const c_char {
let message_str = chars_to_string(message);
let checksum = crc16_ccitt(&message_str);
drop(message_str);
to_c_char(&checksum)
}

Expand All @@ -55,14 +58,15 @@ pub extern "C" fn crc16_ccitt_from_message(message: *const c_char) -> *const c_c
pub extern "C" fn edn_from_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode = str_to_brcode(&edn_str);
drop(edn);
to_c_char(&edn_rs::to_string(brcode))
}

#[no_mangle]
pub extern "C" fn edn_to_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap();

drop(edn);
to_c_char(&brcode_to_string(brcode))
}

Expand All @@ -71,7 +75,7 @@ pub extern "C" fn edn_to_svg_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap();
let svg = brcode.to_svg_standard_string();

drop(edn);
to_c_char(&svg)
}

Expand All @@ -80,6 +84,7 @@ pub extern "C" fn edn_to_svg_file(edn: *const c_char, file_path: *const c_char)
let edn_str = chars_to_string(edn);
let file_path_str = chars_to_string(file_path);
let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap();
drop(edn); drop(file_path);
brcode.to_standard_svg_file(&file_path_str);
}

Expand All @@ -88,14 +93,15 @@ pub extern "C" fn edn_to_svg_file(edn: *const c_char, file_path: *const c_char)
pub extern "C" fn json_from_brcode(json: *const c_char) -> *const c_char {
let json_str = chars_to_string(json);
let brcode = str_to_brcode(&json_str);
drop(json);
to_c_char(&serde_json::to_string(&brcode).unwrap_or_else(|_| "error".to_string()))
}

#[no_mangle]
pub extern "C" fn json_to_brcode(json: *const c_char) -> *const c_char {
let json_str = chars_to_string(json);
let brcode: BrCode = serde_json::from_str(&json_str).unwrap();

drop(json);
to_c_char(&brcode_to_string(brcode))
}

Expand All @@ -104,7 +110,7 @@ pub extern "C" fn json_to_svg_brcode(json: *const c_char) -> *const c_char {
let json_str = chars_to_string(json);
let brcode: BrCode = serde_json::from_str(&json_str).unwrap();
let svg = brcode.to_svg_standard_string();

drop(json);
to_c_char(&svg)
}

Expand All @@ -113,5 +119,6 @@ pub extern "C" fn json_to_svg_file(json: *const c_char, file_path: *const c_char
let json_str = chars_to_string(json);
let file_path_str = chars_to_string(file_path);
let brcode: BrCode = serde_json::from_str(&json_str).unwrap();
drop(json); drop(file_path);
brcode.to_standard_svg_file(&file_path_str);
}

0 comments on commit 01893fe

Please sign in to comment.