Skip to content

Commit c328558

Browse files
authored
Implement From<&str> and From<&[u8]> for CryptoVec (#391)
1 parent b1afeba commit c328558

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed

cryptovec/src/cryptovec.rs

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ impl From<String> for CryptoVec {
4747
}
4848
}
4949

50+
impl From<&str> for CryptoVec {
51+
fn from(e: &str) -> Self {
52+
CryptoVec::from(e.as_bytes())
53+
}
54+
}
55+
56+
impl From<&[u8]> for CryptoVec {
57+
fn from(e: &[u8]) -> Self {
58+
CryptoVec::from_slice(e)
59+
}
60+
}
61+
5062
impl From<Vec<u8>> for CryptoVec {
5163
fn from(e: Vec<u8>) -> Self {
5264
let mut c = CryptoVec::new_zeroed(e.len());
@@ -356,25 +368,18 @@ impl Drop for CryptoVec {
356368
}
357369
}
358370

359-
// DocTests cannot be run on with wasm_bindgen_test
360371
#[cfg(test)]
361-
#[cfg(target_arch = "wasm32")]
362372
mod test {
363-
364-
use wasm_bindgen_test::wasm_bindgen_test;
365-
366373
use super::CryptoVec;
367374

368-
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
369-
370-
#[wasm_bindgen_test]
375+
#[test]
371376
fn test_new() {
372377
let crypto_vec = CryptoVec::new();
373378
assert_eq!(crypto_vec.size, 0);
374379
assert_eq!(crypto_vec.capacity, 0);
375380
}
376381

377-
#[wasm_bindgen_test]
382+
#[test]
378383
fn test_resize_expand() {
379384
let mut crypto_vec = CryptoVec::new_zeroed(5);
380385
crypto_vec.resize(10);
@@ -383,7 +388,7 @@ mod test {
383388
assert!(crypto_vec.iter().skip(5).all(|&x| x == 0)); // Ensure newly added elements are zeroed
384389
}
385390

386-
#[wasm_bindgen_test]
391+
#[test]
387392
fn test_resize_shrink() {
388393
let mut crypto_vec = CryptoVec::new_zeroed(10);
389394
crypto_vec.resize(5);
@@ -392,7 +397,7 @@ mod test {
392397
assert_eq!(crypto_vec.len(), 5);
393398
}
394399

395-
#[wasm_bindgen_test]
400+
#[test]
396401
fn test_push() {
397402
let mut crypto_vec = CryptoVec::new();
398403
crypto_vec.push(1);
@@ -402,7 +407,7 @@ mod test {
402407
assert_eq!(crypto_vec[1], 2);
403408
}
404409

405-
#[wasm_bindgen_test]
410+
#[test]
406411
fn test_write_trait() {
407412
use std::io::Write;
408413

@@ -413,7 +418,7 @@ mod test {
413418
assert_eq!(crypto_vec.as_ref(), &[1, 2, 3]);
414419
}
415420

416-
#[wasm_bindgen_test]
421+
#[test]
417422
fn test_as_ref_as_mut() {
418423
let mut crypto_vec = CryptoVec::new_zeroed(5);
419424
let slice_ref: &[u8] = crypto_vec.as_ref();
@@ -423,29 +428,43 @@ mod test {
423428
assert_eq!(crypto_vec[0], 1);
424429
}
425430

426-
#[wasm_bindgen_test]
431+
#[test]
427432
fn test_from_string() {
428433
let input = String::from("hello");
429434
let crypto_vec: CryptoVec = input.into();
430435
assert_eq!(crypto_vec.as_ref(), b"hello");
431436
}
432437

433-
#[wasm_bindgen_test]
438+
#[test]
439+
fn test_from_str() {
440+
let input = "hello";
441+
let crypto_vec: CryptoVec = input.into();
442+
assert_eq!(crypto_vec.as_ref(), b"hello");
443+
}
444+
445+
#[test]
446+
fn test_from_byte_slice() {
447+
let input = b"hello".as_slice();
448+
let crypto_vec: CryptoVec = input.into();
449+
assert_eq!(crypto_vec.as_ref(), b"hello");
450+
}
451+
452+
#[test]
434453
fn test_from_vec() {
435454
let input = vec![1, 2, 3, 4];
436455
let crypto_vec: CryptoVec = input.into();
437456
assert_eq!(crypto_vec.as_ref(), &[1, 2, 3, 4]);
438457
}
439458

440-
#[wasm_bindgen_test]
459+
#[test]
441460
fn test_index() {
442461
let crypto_vec = CryptoVec::from(vec![1, 2, 3, 4, 5]);
443462
assert_eq!(crypto_vec[0], 1);
444463
assert_eq!(crypto_vec[4], 5);
445464
assert_eq!(&crypto_vec[1..3], &[2, 3]);
446465
}
447466

448-
#[wasm_bindgen_test]
467+
#[test]
449468
fn test_drop() {
450469
let mut crypto_vec = CryptoVec::new_zeroed(10);
451470
// Ensure vector is filled with non-zero data
@@ -458,48 +477,30 @@ mod test {
458477
// it may be checked using tools like Valgrind or manual inspection.
459478
}
460479

461-
#[wasm_bindgen_test]
480+
#[test]
462481
fn test_new_zeroed() {
463482
let crypto_vec = CryptoVec::new_zeroed(10);
464483
assert_eq!(crypto_vec.size, 10);
465484
assert!(crypto_vec.capacity >= 10);
466485
assert!(crypto_vec.iter().all(|&x| x == 0)); // Ensure all bytes are zeroed
467486
}
468487

469-
#[wasm_bindgen_test]
470-
fn test_push_u32_be() {
471-
let mut crypto_vec = CryptoVec::new();
472-
let value = 43554u32;
473-
crypto_vec.push_u32_be(value);
474-
assert_eq!(crypto_vec.len(), 4); // u32 is 4 bytes long
475-
assert_eq!(crypto_vec.read_u32_be(0), value);
476-
}
477-
478-
#[wasm_bindgen_test]
479-
480-
fn test_read_u32_be() {
481-
let mut crypto_vec = CryptoVec::new();
482-
let value = 99485710u32;
483-
crypto_vec.push_u32_be(value);
484-
assert_eq!(crypto_vec.read_u32_be(0), value);
485-
}
486-
487-
#[wasm_bindgen_test]
488+
#[test]
488489
fn test_clear() {
489490
let mut crypto_vec = CryptoVec::new();
490491
crypto_vec.extend(b"blabla");
491492
crypto_vec.clear();
492493
assert!(crypto_vec.is_empty());
493494
}
494495

495-
#[wasm_bindgen_test]
496+
#[test]
496497
fn test_extend() {
497498
let mut crypto_vec = CryptoVec::new();
498499
crypto_vec.extend(b"test");
499500
assert_eq!(crypto_vec.as_ref(), b"test");
500501
}
501502

502-
#[wasm_bindgen_test]
503+
#[test]
503504
fn test_write_all_from() {
504505
let mut crypto_vec = CryptoVec::new();
505506
crypto_vec.extend(b"blabla");
@@ -510,10 +511,37 @@ mod test {
510511
assert_eq!(output, b"blabla");
511512
}
512513

513-
#[wasm_bindgen_test]
514+
#[test]
514515
fn test_resize_mut() {
515516
let mut crypto_vec = CryptoVec::new();
516517
crypto_vec.resize_mut(4).clone_from_slice(b"test");
517518
assert_eq!(crypto_vec.as_ref(), b"test");
518519
}
520+
521+
// DocTests cannot be run on with wasm_bindgen_test
522+
#[cfg(target_arch = "wasm32")]
523+
mod wasm32 {
524+
use wasm_bindgen_test::wasm_bindgen_test;
525+
526+
use super::*;
527+
528+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
529+
530+
#[wasm_bindgen_test]
531+
fn test_push_u32_be() {
532+
let mut crypto_vec = CryptoVec::new();
533+
let value = 43554u32;
534+
crypto_vec.push_u32_be(value);
535+
assert_eq!(crypto_vec.len(), 4); // u32 is 4 bytes long
536+
assert_eq!(crypto_vec.read_u32_be(0), value);
537+
}
538+
539+
#[wasm_bindgen_test]
540+
fn test_read_u32_be() {
541+
let mut crypto_vec = CryptoVec::new();
542+
let value = 99485710u32;
543+
crypto_vec.push_u32_be(value);
544+
assert_eq!(crypto_vec.read_u32_be(0), value);
545+
}
546+
}
519547
}

0 commit comments

Comments
 (0)