1use std::{
2 borrow::{Borrow, BorrowMut, Cow},
3 cmp::Ordering,
4 convert::Infallible,
5 ffi::OsStr,
6 fmt::{self, Debug, Display},
7 hash::{Hash, Hasher},
8 net::{SocketAddr, ToSocketAddrs},
9 ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut},
10 path::Path,
11 slice::SliceIndex,
12 str::{FromStr, Utf8Error},
13};
14
15use bytes::{Bytes, BytesMut};
16
17#[derive(Clone, Default, PartialEq, Eq)]
28pub struct BytesString {
29 pub(crate) bytes: BytesMut,
30}
31
32impl BytesString {
33 pub fn new() -> Self {
45 Self {
46 bytes: BytesMut::new(),
47 }
48 }
49
50 pub fn with_capacity(capacity: usize) -> Self {
66 Self {
67 bytes: BytesMut::with_capacity(capacity),
68 }
69 }
70
71 pub fn len(&self) -> usize {
83 self.bytes.len()
84 }
85
86 pub fn capacity(&self) -> usize {
98 self.bytes.capacity()
99 }
100
101 pub fn reserve(&mut self, additional: usize) {
120 self.bytes.reserve(additional);
121 }
122
123 pub fn split_off(&mut self, at: usize) -> Self {
146 assert!(self.is_char_boundary(at));
147
148 Self {
149 bytes: self.bytes.split_off(at),
150 }
151 }
152
153 pub fn as_bytes(&self) -> &[u8] {
165 self.bytes.as_ref()
166 }
167
168 pub fn is_empty(&self) -> bool {
180 self.bytes.is_empty()
181 }
182
183 pub fn truncate(&mut self, new_len: usize) {
214 if new_len <= self.len() {
215 assert!(self.is_char_boundary(new_len));
216 self.bytes.truncate(new_len);
217 }
218 }
219
220 pub fn clear(&mut self) {
234 self.bytes.clear();
235 }
236
237 pub fn push(&mut self, ch: char) {
251 let mut buf = [0; 4];
252 let bytes = ch.encode_utf8(&mut buf);
253 self.bytes.extend_from_slice(bytes.as_bytes());
254 }
255
256 pub fn push_str(&mut self, s: &str) {
270 self.bytes.extend_from_slice(s.as_bytes());
271 }
272
273 pub fn as_str(&self) -> &str {
285 unsafe { std::str::from_utf8_unchecked(&self.bytes) }
286 }
287
288 pub fn as_mut_str(&mut self) -> &mut str {
302 unsafe { std::str::from_utf8_unchecked_mut(&mut self.bytes) }
303 }
304
305 pub fn into_bytes(self) -> BytesMut {
320 self.bytes
321 }
322
323 pub unsafe fn from_bytes_unchecked(bytes: BytesMut) -> Self {
331 Self { bytes }
332 }
333
334 pub fn from_utf8(bytes: BytesMut) -> Result<Self, Utf8Error> {
349 std::str::from_utf8(bytes.as_ref())?;
350
351 Ok(Self { bytes })
352 }
353
354 pub fn from_utf8_slice(bytes: &[u8]) -> Result<Self, Utf8Error> {
369 std::str::from_utf8(bytes)?;
370
371 Ok(Self {
372 bytes: BytesMut::from(bytes),
373 })
374 }
375
376 pub fn into_vec(self) -> Vec<u8> {
389 self.bytes.into()
390 }
391
392 pub fn into_string(self) -> String {
407 self.into()
408 }
409}
410
411impl Deref for BytesString {
412 type Target = str;
413
414 fn deref(&self) -> &Self::Target {
415 self.as_str()
416 }
417}
418
419impl DerefMut for BytesString {
420 fn deref_mut(&mut self) -> &mut Self::Target {
421 self.as_mut_str()
422 }
423}
424
425impl AsRef<str> for BytesString {
426 fn as_ref(&self) -> &str {
427 self.as_str()
428 }
429}
430
431impl Borrow<str> for BytesString {
432 fn borrow(&self) -> &str {
433 self.as_str()
434 }
435}
436
437impl From<String> for BytesString {
438 fn from(s: String) -> Self {
439 Self {
440 bytes: Bytes::from(s.into_bytes()).into(),
441 }
442 }
443}
444
445impl From<BytesString> for String {
446 fn from(s: BytesString) -> Self {
447 let vec: Vec<_> = s.bytes.freeze().into();
448
449 unsafe {
450 String::from_utf8_unchecked(vec)
452 }
453 }
454}
455
456impl From<&str> for BytesString {
457 fn from(s: &str) -> Self {
458 Self {
459 bytes: BytesMut::from(s),
460 }
461 }
462}
463
464impl From<BytesString> for BytesMut {
465 fn from(s: BytesString) -> Self {
466 s.bytes
467 }
468}
469
470impl From<BytesString> for Bytes {
471 fn from(s: BytesString) -> Self {
472 s.bytes.into()
473 }
474}
475
476impl From<char> for BytesString {
477 fn from(ch: char) -> Self {
478 let mut bytes = BytesString::with_capacity(ch.len_utf8());
479 bytes.push(ch);
480 bytes
481 }
482}
483
484impl PartialEq<str> for BytesString {
485 fn eq(&self, other: &str) -> bool {
486 self.as_str() == other
487 }
488}
489
490impl PartialEq<&'_ str> for BytesString {
491 fn eq(&self, other: &&str) -> bool {
492 self.as_str() == *other
493 }
494}
495
496impl PartialEq<Cow<'_, str>> for BytesString {
497 fn eq(&self, other: &Cow<'_, str>) -> bool {
498 self.as_str() == *other
499 }
500}
501
502impl PartialEq<BytesString> for str {
503 fn eq(&self, other: &BytesString) -> bool {
504 self == other.as_str()
505 }
506}
507
508impl PartialEq<BytesString> for &'_ str {
509 fn eq(&self, other: &BytesString) -> bool {
510 *self == other.as_str()
511 }
512}
513
514impl PartialEq<BytesString> for Bytes {
515 fn eq(&self, other: &BytesString) -> bool {
516 self == other.as_bytes()
517 }
518}
519
520impl PartialEq<String> for BytesString {
521 fn eq(&self, other: &String) -> bool {
522 self.as_str() == other
523 }
524}
525
526impl PartialEq<BytesString> for String {
527 fn eq(&self, other: &BytesString) -> bool {
528 self == other.as_str()
529 }
530}
531
532impl Add<&str> for BytesString {
533 type Output = Self;
534
535 fn add(mut self, other: &str) -> Self::Output {
536 self += other;
537 self
538 }
539}
540
541impl AddAssign<&str> for BytesString {
542 fn add_assign(&mut self, other: &str) {
543 self.push_str(other);
544 }
545}
546
547impl Add<BytesString> for BytesString {
548 type Output = Self;
549
550 fn add(mut self, other: BytesString) -> Self::Output {
551 self += other;
552 self
553 }
554}
555
556impl AddAssign<BytesString> for BytesString {
557 fn add_assign(&mut self, other: BytesString) {
558 self.bytes.extend(other.bytes);
559 }
560}
561
562impl AsMut<str> for BytesString {
563 fn as_mut(&mut self) -> &mut str {
564 self.as_mut_str()
565 }
566}
567
568impl AsRef<[u8]> for BytesString {
569 fn as_ref(&self) -> &[u8] {
570 self.as_bytes()
571 }
572}
573
574impl AsRef<OsStr> for BytesString {
575 fn as_ref(&self) -> &OsStr {
576 OsStr::new(self.as_str())
577 }
578}
579
580impl AsRef<Path> for BytesString {
581 fn as_ref(&self) -> &Path {
582 Path::new(self.as_str())
583 }
584}
585
586impl BorrowMut<str> for BytesString {
587 fn borrow_mut(&mut self) -> &mut str {
588 self.as_mut_str()
589 }
590}
591
592impl Debug for BytesString {
593 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594 Debug::fmt(self.as_str(), f)
595 }
596}
597
598impl Display for BytesString {
599 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
600 Display::fmt(self.as_str(), f)
601 }
602}
603
604impl PartialOrd for BytesString {
605 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
606 Some(self.cmp(other))
607 }
608}
609
610impl Ord for BytesString {
611 fn cmp(&self, other: &Self) -> Ordering {
612 self.as_str().cmp(other.as_str())
613 }
614}
615
616impl<'a> Extend<&'a char> for BytesString {
617 fn extend<T: IntoIterator<Item = &'a char>>(&mut self, iter: T) {
618 self.extend(iter.into_iter().copied());
619 }
620}
621
622impl Extend<char> for BytesString {
623 fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) {
624 let mut buf = [0; 4];
625 for ch in iter {
626 let bytes = ch.encode_utf8(&mut buf);
627 self.bytes.extend_from_slice(bytes.as_bytes());
628 }
629 }
630}
631
632impl<'a> Extend<&'a str> for BytesString {
633 fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
634 for s in iter {
635 self.push_str(s);
636 }
637 }
638}
639
640impl<'a, 'b> Extend<&'a &'b str> for BytesString {
641 fn extend<T: IntoIterator<Item = &'a &'b str>>(&mut self, iter: T) {
642 for s in iter {
643 self.push_str(s);
644 }
645 }
646}
647
648impl Extend<Box<str>> for BytesString {
649 fn extend<T: IntoIterator<Item = Box<str>>>(&mut self, iter: T) {
650 for s in iter {
651 self.push_str(&s);
652 }
653 }
654}
655
656impl<'a> Extend<Cow<'a, str>> for BytesString {
657 fn extend<T: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: T) {
658 for s in iter {
659 self.push_str(&s);
660 }
661 }
662}
663
664impl Extend<String> for BytesString {
665 fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) {
666 for s in iter {
667 self.push_str(&s);
668 }
669 }
670}
671
672impl<'a> Extend<&'a String> for BytesString {
673 fn extend<T: IntoIterator<Item = &'a String>>(&mut self, iter: T) {
674 for s in iter {
675 self.push_str(s);
676 }
677 }
678}
679
680impl Extend<BytesString> for BytesString {
681 fn extend<T: IntoIterator<Item = BytesString>>(&mut self, iter: T) {
682 for s in iter {
683 self.bytes.extend(s.bytes);
684 }
685 }
686}
687
688impl FromIterator<char> for BytesString {
689 fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
690 let mut bytes = BytesString::new();
691 bytes.extend(iter);
692 bytes
693 }
694}
695
696impl<'a> FromIterator<&'a str> for BytesString {
697 fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
698 let mut bytes = BytesString::new();
699 bytes.extend(iter);
700 bytes
701 }
702}
703
704impl FromIterator<Box<str>> for BytesString {
705 fn from_iter<T: IntoIterator<Item = Box<str>>>(iter: T) -> Self {
706 let mut bytes = BytesString::new();
707 bytes.extend(iter);
708 bytes
709 }
710}
711
712impl<'a> FromIterator<Cow<'a, str>> for BytesString {
713 fn from_iter<T: IntoIterator<Item = Cow<'a, str>>>(iter: T) -> Self {
714 let mut bytes = BytesString::new();
715 bytes.extend(iter);
716 bytes
717 }
718}
719
720impl FromIterator<String> for BytesString {
721 fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
722 let mut bytes = BytesString::new();
723 bytes.extend(iter);
724 bytes
725 }
726}
727
728impl FromIterator<BytesString> for BytesString {
729 fn from_iter<T: IntoIterator<Item = BytesString>>(iter: T) -> Self {
730 let mut bytes = BytesString::new();
731 bytes.extend(iter);
732 bytes
733 }
734}
735
736impl FromStr for BytesString {
737 type Err = Infallible;
738
739 fn from_str(s: &str) -> Result<Self, Self::Err> {
740 Ok(Self {
741 bytes: BytesMut::from(s),
742 })
743 }
744}
745
746impl<I> Index<I> for BytesString
747where
748 I: SliceIndex<str>,
749{
750 type Output = I::Output;
751
752 fn index(&self, index: I) -> &Self::Output {
753 self.as_str().index(index)
754 }
755}
756
757impl<I> IndexMut<I> for BytesString
758where
759 I: SliceIndex<str>,
760{
761 fn index_mut(&mut self, index: I) -> &mut Self::Output {
762 self.as_mut_str().index_mut(index)
763 }
764}
765
766impl ToSocketAddrs for BytesString {
767 type Iter = std::vec::IntoIter<SocketAddr>;
768
769 fn to_socket_addrs(&self) -> Result<Self::Iter, std::io::Error> {
770 self.as_str().to_socket_addrs()
771 }
772}
773
774impl Hash for BytesString {
776 fn hash<H: Hasher>(&self, state: &mut H) {
777 self.as_str().hash(state);
778 }
779}
780
781impl fmt::Write for BytesString {
782 fn write_str(&mut self, s: &str) -> fmt::Result {
783 self.push_str(s);
784 Ok(())
785 }
786
787 fn write_char(&mut self, c: char) -> fmt::Result {
788 self.push(c);
789 Ok(())
790 }
791}
792
793#[cfg(feature = "serde")]
794mod serde_impl {
795 use serde::{Deserialize, Deserializer, Serialize, Serializer};
796
797 use super::*;
798
799 impl<'de> Deserialize<'de> for BytesString {
800 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
801 where
802 D: Deserializer<'de>,
803 {
804 let s = String::deserialize(deserializer)?;
805 Ok(Self::from(s))
806 }
807 }
808
809 impl Serialize for BytesString {
810 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
811 where
812 S: Serializer,
813 {
814 serializer.serialize_str(self.as_str())
815 }
816 }
817}
818
819#[cfg(test)]
820mod tests {
821 use std::{
822 collections::{hash_map::DefaultHasher, HashMap},
823 hash::{Hash, Hasher},
824 };
825
826 use super::*;
827
828 #[test]
829 fn test_new() {
830 let s = BytesString::new();
831 assert!(s.is_empty());
832 assert_eq!(s.len(), 0);
833 assert_eq!(s.as_str(), "");
834 }
835
836 #[test]
837 fn test_with_capacity() {
838 let s = BytesString::with_capacity(10);
839 assert!(s.capacity() >= 10);
840 assert!(s.is_empty());
841 assert_eq!(s.len(), 0);
842 }
843
844 #[test]
845 fn test_from_str() {
846 let s = BytesString::from("hello");
847 assert_eq!(s.as_str(), "hello");
848 assert_eq!(s.len(), 5);
849 assert!(!s.is_empty());
850 }
851
852 #[test]
853 fn test_from_string() {
854 let original = String::from("hello world");
855 let s = BytesString::from(original);
856 assert_eq!(s.as_str(), "hello world");
857 assert_eq!(s.len(), 11);
858 }
859
860 #[test]
861 fn test_from_char() {
862 let s = BytesString::from('H');
863 assert_eq!(s.as_str(), "H");
864 assert_eq!(s.len(), 1);
865
866 let s = BytesString::from('한');
868 assert_eq!(s.as_str(), "한");
869 assert_eq!(s.len(), 3); }
871
872 #[test]
873 fn test_push() {
874 let mut s = BytesString::from("hello");
875 s.push(' ');
876 s.push('w');
877 assert_eq!(s.as_str(), "hello w");
878
879 s.push('한');
881 assert_eq!(s.as_str(), "hello w한");
882 }
883
884 #[test]
885 fn test_push_str() {
886 let mut s = BytesString::from("hello");
887 s.push_str(" world");
888 assert_eq!(s.as_str(), "hello world");
889
890 s.push_str("!");
891 assert_eq!(s.as_str(), "hello world!");
892
893 s.push_str(" 한국어");
895 assert_eq!(s.as_str(), "hello world! 한국어");
896 }
897
898 #[test]
899 fn test_clear() {
900 let mut s = BytesString::from("hello");
901 assert!(!s.is_empty());
902 s.clear();
903 assert!(s.is_empty());
904 assert_eq!(s.len(), 0);
905 assert_eq!(s.as_str(), "");
906 }
907
908 #[test]
909 fn test_truncate() {
910 let mut s = BytesString::from("hello world");
911 s.truncate(5);
912 assert_eq!(s.as_str(), "hello");
913
914 s.truncate(20);
916 assert_eq!(s.as_str(), "hello");
917
918 let mut s = BytesString::from("한국어");
920 s.truncate(6); assert_eq!(s.as_str(), "한국");
922 }
923
924 #[test]
925 #[should_panic]
926 fn test_truncate_panic_on_char_boundary() {
927 let mut s = BytesString::from("한국어");
928 s.truncate(1); }
930
931 #[test]
932 fn test_split_off() {
933 let mut s = BytesString::from("hello world");
934 let other = s.split_off(6);
935 assert_eq!(s.as_str(), "hello ");
936 assert_eq!(other.as_str(), "world");
937
938 let mut s = BytesString::from("hello");
940 let other = s.split_off(0);
941 assert_eq!(s.as_str(), "");
942 assert_eq!(other.as_str(), "hello");
943
944 let mut s = BytesString::from("hello");
946 let other = s.split_off(5);
947 assert_eq!(s.as_str(), "hello");
948 assert_eq!(other.as_str(), "");
949
950 let mut s = BytesString::from("한국어");
952 let other = s.split_off(6);
953 assert_eq!(s.as_str(), "한국");
954 assert_eq!(other.as_str(), "어");
955 }
956
957 #[test]
958 #[should_panic]
959 fn test_split_off_panic_on_char_boundary() {
960 let mut s = BytesString::from("é");
961 s.split_off(1); }
963
964 #[test]
965 fn test_as_bytes() {
966 let s = BytesString::from("hello");
967 assert_eq!(s.as_bytes(), b"hello");
968
969 let s = BytesString::from("한국어");
970 assert_eq!(s.as_bytes(), "한국어".as_bytes());
971 }
972
973 #[test]
974 fn test_as_mut_str() {
975 let mut s = BytesString::from("hello");
976 s.as_mut_str().make_ascii_uppercase();
977 assert_eq!(s.as_str(), "HELLO");
978 }
979
980 #[test]
981 fn test_into_bytes() {
982 let s = BytesString::from("hello");
983 let bytes = s.into_bytes();
984 assert_eq!(bytes.as_ref(), b"hello");
985 }
986
987 #[test]
988 fn test_from_utf8() {
989 let bytes = BytesMut::from(&b"hello"[..]);
990 let s = BytesString::from_utf8(bytes).unwrap();
991 assert_eq!(s.as_str(), "hello");
992
993 let invalid_bytes = BytesMut::from(&[0xff, 0xfe][..]);
995 assert!(BytesString::from_utf8(invalid_bytes).is_err());
996 }
997
998 #[test]
999 fn test_from_utf8_slice() {
1000 let s = BytesString::from_utf8_slice(b"hello").unwrap();
1001 assert_eq!(s.as_str(), "hello");
1002
1003 assert!(BytesString::from_utf8_slice(&[0xff, 0xfe]).is_err());
1005 }
1006
1007 #[test]
1008 fn test_from_bytes_unchecked() {
1009 let bytes = BytesMut::from(&b"hello"[..]);
1010 let s = unsafe { BytesString::from_bytes_unchecked(bytes) };
1011 assert_eq!(s.as_str(), "hello");
1012 }
1013
1014 #[test]
1015 fn test_reserve() {
1016 let mut s = BytesString::from("hello");
1017 let initial_capacity = s.capacity();
1018 s.reserve(100);
1019 assert!(s.capacity() >= initial_capacity + 100);
1020 assert_eq!(s.as_str(), "hello"); }
1022
1023 #[test]
1024 fn test_deref() {
1025 let s = BytesString::from("hello world");
1026 assert_eq!(s.len(), 11);
1027 assert!(s.contains("world"));
1028 assert!(s.starts_with("hello"));
1029 assert!(s.ends_with("world"));
1030 }
1031
1032 #[test]
1033 fn test_partial_eq() {
1034 let s = BytesString::from("hello");
1035
1036 assert_eq!(s, "hello");
1038 assert_ne!(s, "world");
1039
1040 assert_eq!(s, String::from("hello"));
1042 assert_ne!(s, String::from("world"));
1043
1044 assert_eq!(s, Cow::Borrowed("hello"));
1046 assert_eq!(s, Cow::Owned(String::from("hello")));
1047
1048 assert_eq!(Bytes::from("hello"), s);
1050 assert_ne!(Bytes::from("world"), s);
1051
1052 assert_eq!("hello", s);
1054 assert_eq!(String::from("hello"), s);
1055 }
1056
1057 #[test]
1058 fn test_ordering() {
1059 let s1 = BytesString::from("apple");
1060 let s2 = BytesString::from("banana");
1061 let s3 = BytesString::from("apple");
1062
1063 assert!(s1 < s2);
1064 assert!(s2 > s1);
1065 assert_eq!(s1, s3);
1066 assert!(s1 <= s3);
1067 assert!(s1 >= s3);
1068 }
1069
1070 #[test]
1071 fn test_hash() {
1072 let s1 = BytesString::from("hello");
1073 let s2 = BytesString::from("hello");
1074 let s3 = BytesString::from("world");
1075
1076 let mut hasher1 = DefaultHasher::new();
1077 let mut hasher2 = DefaultHasher::new();
1078 let mut hasher3 = DefaultHasher::new();
1079
1080 s1.hash(&mut hasher1);
1081 s2.hash(&mut hasher2);
1082 s3.hash(&mut hasher3);
1083
1084 assert_eq!(hasher1.finish(), hasher2.finish());
1085 assert_ne!(hasher1.finish(), hasher3.finish());
1086
1087 let mut str_hasher = DefaultHasher::new();
1089 "hello".hash(&mut str_hasher);
1090 assert_eq!(hasher1.finish(), str_hasher.finish());
1091 }
1092
1093 #[test]
1094 fn test_add() {
1095 let s1 = BytesString::from("hello");
1096 let s2 = s1 + " world";
1097 assert_eq!(s2.as_str(), "hello world");
1098
1099 let s3 = BytesString::from("foo");
1100 let s4 = BytesString::from("bar");
1101 let s5 = s3 + s4;
1102 assert_eq!(s5.as_str(), "foobar");
1103 }
1104
1105 #[test]
1106 fn test_add_assign() {
1107 let mut s = BytesString::from("hello");
1108 s += " world";
1109 assert_eq!(s.as_str(), "hello world");
1110
1111 let mut s1 = BytesString::from("foo");
1112 let s2 = BytesString::from("bar");
1113 s1 += s2;
1114 assert_eq!(s1.as_str(), "foobar");
1115 }
1116
1117 #[test]
1118 fn test_extend_char() {
1119 let mut s = BytesString::from("hello");
1120 s.extend(['!', ' ', '🎉'].iter());
1121 assert_eq!(s.as_str(), "hello! 🎉");
1122
1123 let mut s = BytesString::new();
1124 s.extend(['a', 'b', 'c']);
1125 assert_eq!(s.as_str(), "abc");
1126 }
1127
1128 #[test]
1129 fn test_extend_str() {
1130 let mut s = BytesString::from("hello");
1131 s.extend([" ", "world", "!"].iter());
1132 assert_eq!(s.as_str(), "hello world!");
1133
1134 let strings = vec![String::from("foo"), String::from("bar")];
1135 let mut s = BytesString::new();
1136 s.extend(&strings);
1137 assert_eq!(s.as_str(), "foobar");
1138 }
1139
1140 #[test]
1141 fn test_extend_bytes_string() {
1142 let mut s = BytesString::from("hello");
1143 let parts = vec![BytesString::from(" "), BytesString::from("world")];
1144 s.extend(parts);
1145 assert_eq!(s.as_str(), "hello world");
1146 }
1147
1148 #[test]
1149 fn test_from_iterator() {
1150 let s: BytesString = ['h', 'e', 'l', 'l', 'o'].into_iter().collect();
1151 assert_eq!(s.as_str(), "hello");
1152
1153 let s: BytesString = ["hello", " ", "world"].into_iter().collect();
1154 assert_eq!(s.as_str(), "hello world");
1155
1156 let strings = vec![String::from("foo"), String::from("bar")];
1157 let s: BytesString = strings.into_iter().collect();
1158 assert_eq!(s.as_str(), "foobar");
1159 }
1160
1161 #[test]
1162 fn test_from_str_trait() {
1163 let s: BytesString = "hello world".parse().unwrap();
1164 assert_eq!(s.as_str(), "hello world");
1165 }
1166
1167 #[test]
1168 fn test_index() {
1169 let s = BytesString::from("hello world");
1170 assert_eq!(&s[0..5], "hello");
1171 assert_eq!(&s[6..], "world");
1172 assert_eq!(&s[..5], "hello");
1173 assert_eq!(&s[6..11], "world");
1174 }
1175
1176 #[test]
1177 fn test_index_mut() {
1178 let mut s = BytesString::from("hello world");
1179 s[0..5].make_ascii_uppercase();
1180 assert_eq!(s.as_str(), "HELLO world");
1181 }
1182
1183 #[test]
1184 fn test_as_ref_implementations() {
1185 let s = BytesString::from("hello/world");
1186
1187 let str_ref: &str = s.as_ref();
1189 assert_eq!(str_ref, "hello/world");
1190
1191 let bytes_ref: &[u8] = s.as_ref();
1193 assert_eq!(bytes_ref, b"hello/world");
1194
1195 let os_str_ref: &OsStr = s.as_ref();
1197 assert_eq!(os_str_ref, OsStr::new("hello/world"));
1198
1199 let path_ref: &Path = s.as_ref();
1201 assert_eq!(path_ref, Path::new("hello/world"));
1202 }
1203
1204 #[test]
1205 fn test_borrow() {
1206 let s = BytesString::from("hello");
1207 let borrowed: &str = s.borrow();
1208 assert_eq!(borrowed, "hello");
1209
1210 let mut s = BytesString::from("hello");
1211 let borrowed_mut: &mut str = s.borrow_mut();
1212 borrowed_mut.make_ascii_uppercase();
1213 assert_eq!(s.as_str(), "HELLO");
1214 }
1215
1216 #[test]
1217 fn test_debug() {
1218 let s = BytesString::from("hello");
1219 assert_eq!(format!("{:?}", s), "\"hello\"");
1220 }
1221
1222 #[test]
1223 fn test_display() {
1224 let s = BytesString::from("hello world");
1225 assert_eq!(format!("{}", s), "hello world");
1226 }
1227
1228 #[test]
1229 fn test_conversions() {
1230 let s = BytesString::from("hello");
1231
1232 let bytes_mut: BytesMut = s.clone().into();
1234 assert_eq!(bytes_mut.as_ref(), b"hello");
1235
1236 let bytes: Bytes = s.into();
1238 assert_eq!(bytes.as_ref(), b"hello");
1239 }
1240
1241 #[test]
1242 fn test_to_socket_addrs() {
1243 let s = BytesString::from("127.0.0.1:8080");
1244 let addrs: Vec<_> = s.to_socket_addrs().unwrap().collect();
1245 assert!(!addrs.is_empty());
1246
1247 let s = BytesString::from("localhost:8080");
1248 let result = s.to_socket_addrs();
1249 let _ = result;
1252 }
1253
1254 #[test]
1255 fn test_unicode_handling() {
1256 let s = BytesString::from("Hello 🌍 한국어 🎉");
1257 assert_eq!(s.as_str(), "Hello 🌍 한국어 🎉");
1258 assert!(s.len() > 13); let mut s = BytesString::new();
1261 s.push('🌍');
1262 s.push_str(" 한국어");
1263 assert_eq!(s.as_str(), "🌍 한국어");
1264 }
1265
1266 #[test]
1267 fn test_empty_operations() {
1268 let mut s = BytesString::new();
1269 assert!(s.is_empty());
1270
1271 s.push_str("");
1272 assert!(s.is_empty());
1273
1274 s.clear();
1275 assert!(s.is_empty());
1276
1277 let other = s.split_off(0);
1278 assert!(s.is_empty());
1279 assert!(other.is_empty());
1280 }
1281
1282 #[test]
1283 fn test_large_string() {
1284 let large_str = "a".repeat(10000);
1285 let s = BytesString::from(large_str.as_str());
1286 assert_eq!(s.len(), 10000);
1287 assert_eq!(s.as_str(), large_str);
1288
1289 let mut s = BytesString::with_capacity(10000);
1290 for _ in 0..10000 {
1291 s.push('a');
1292 }
1293 assert_eq!(s.len(), 10000);
1294 assert_eq!(s.as_str(), large_str);
1295 }
1296
1297 #[test]
1298 fn test_clone() {
1299 let s1 = BytesString::from("hello world");
1300 let s2 = s1.clone();
1301 assert_eq!(s1, s2);
1302 assert_eq!(s1.as_str(), s2.as_str());
1303 }
1304
1305 #[test]
1306 fn test_default() {
1307 let s: BytesString = Default::default();
1308 assert!(s.is_empty());
1309 assert_eq!(s.as_str(), "");
1310 }
1311
1312 #[test]
1313 fn test_hash_map_usage() {
1314 let mut map = HashMap::new();
1315 let key = BytesString::from("key");
1316 map.insert(key, "value");
1317
1318 let lookup_key = BytesString::from("key");
1319 assert_eq!(map.get(&lookup_key), Some(&"value"));
1320
1321 assert_eq!(map.get("key"), Some(&"value"));
1323 }
1324}