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 {
142 Self {
143 bytes: self.bytes.split_off(at),
144 }
145 }
146
147 pub fn as_bytes(&self) -> &[u8] {
159 self.bytes.as_ref()
160 }
161
162 pub fn is_empty(&self) -> bool {
174 self.bytes.is_empty()
175 }
176
177 pub fn truncate(&mut self, new_len: usize) {
208 if new_len <= self.len() {
209 assert!(self.is_char_boundary(new_len));
210 self.bytes.truncate(new_len);
211 }
212 }
213
214 pub fn clear(&mut self) {
228 self.bytes.clear();
229 }
230
231 pub fn push(&mut self, ch: char) {
245 let mut buf = [0; 4];
246 let bytes = ch.encode_utf8(&mut buf);
247 self.bytes.extend_from_slice(bytes.as_bytes());
248 }
249
250 pub fn push_str(&mut self, s: &str) {
264 self.bytes.extend_from_slice(s.as_bytes());
265 }
266
267 pub fn as_str(&self) -> &str {
279 unsafe { std::str::from_utf8_unchecked(&self.bytes) }
280 }
281
282 pub fn as_mut_str(&mut self) -> &mut str {
296 unsafe { std::str::from_utf8_unchecked_mut(&mut self.bytes) }
297 }
298
299 pub fn into_bytes(self) -> BytesMut {
314 self.bytes
315 }
316
317 pub unsafe fn from_bytes_unchecked(bytes: BytesMut) -> Self {
325 Self { bytes }
326 }
327
328 pub fn from_utf8(bytes: BytesMut) -> Result<Self, Utf8Error> {
343 std::str::from_utf8(bytes.as_ref())?;
344
345 Ok(Self { bytes })
346 }
347
348 pub fn from_utf8_slice(bytes: &[u8]) -> Result<Self, Utf8Error> {
363 std::str::from_utf8(bytes)?;
364
365 Ok(Self {
366 bytes: BytesMut::from(bytes),
367 })
368 }
369
370 pub fn into_vec(self) -> Vec<u8> {
383 self.bytes.into()
384 }
385
386 pub fn into_string(self) -> String {
401 self.into()
402 }
403}
404
405impl Deref for BytesString {
406 type Target = str;
407
408 fn deref(&self) -> &Self::Target {
409 self.as_str()
410 }
411}
412
413impl DerefMut for BytesString {
414 fn deref_mut(&mut self) -> &mut Self::Target {
415 self.as_mut_str()
416 }
417}
418
419impl AsRef<str> for BytesString {
420 fn as_ref(&self) -> &str {
421 self.as_str()
422 }
423}
424
425impl Borrow<str> for BytesString {
426 fn borrow(&self) -> &str {
427 self.as_str()
428 }
429}
430
431impl From<String> for BytesString {
432 fn from(s: String) -> Self {
433 Self {
434 bytes: Bytes::from(s.into_bytes()).into(),
435 }
436 }
437}
438
439impl From<BytesString> for String {
440 fn from(s: BytesString) -> Self {
441 let vec: Vec<_> = s.bytes.freeze().into();
442
443 unsafe {
444 String::from_utf8_unchecked(vec)
446 }
447 }
448}
449
450impl From<&str> for BytesString {
451 fn from(s: &str) -> Self {
452 Self {
453 bytes: BytesMut::from(s),
454 }
455 }
456}
457
458impl From<BytesString> for BytesMut {
459 fn from(s: BytesString) -> Self {
460 s.bytes
461 }
462}
463
464impl From<BytesString> for Bytes {
465 fn from(s: BytesString) -> Self {
466 s.bytes.into()
467 }
468}
469
470impl From<char> for BytesString {
471 fn from(ch: char) -> Self {
472 let mut bytes = BytesString::with_capacity(ch.len_utf8());
473 bytes.push(ch);
474 bytes
475 }
476}
477
478impl PartialEq<str> for BytesString {
479 fn eq(&self, other: &str) -> bool {
480 self.as_str() == other
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<Cow<'_, str>> for BytesString {
491 fn eq(&self, other: &Cow<'_, str>) -> bool {
492 self.as_str() == *other
493 }
494}
495
496impl PartialEq<BytesString> for str {
497 fn eq(&self, other: &BytesString) -> bool {
498 self == other.as_str()
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 Bytes {
509 fn eq(&self, other: &BytesString) -> bool {
510 self == other.as_bytes()
511 }
512}
513
514impl PartialEq<String> for BytesString {
515 fn eq(&self, other: &String) -> bool {
516 self.as_str() == other
517 }
518}
519
520impl PartialEq<BytesString> for String {
521 fn eq(&self, other: &BytesString) -> bool {
522 self == other.as_str()
523 }
524}
525
526impl Add<&str> for BytesString {
527 type Output = Self;
528
529 fn add(mut self, other: &str) -> Self::Output {
530 self += other;
531 self
532 }
533}
534
535impl AddAssign<&str> for BytesString {
536 fn add_assign(&mut self, other: &str) {
537 self.push_str(other);
538 }
539}
540
541impl Add<BytesString> for BytesString {
542 type Output = Self;
543
544 fn add(mut self, other: BytesString) -> Self::Output {
545 self += other;
546 self
547 }
548}
549
550impl AddAssign<BytesString> for BytesString {
551 fn add_assign(&mut self, other: BytesString) {
552 self.bytes.extend(other.bytes);
553 }
554}
555
556impl AsMut<str> for BytesString {
557 fn as_mut(&mut self) -> &mut str {
558 self.as_mut_str()
559 }
560}
561
562impl AsRef<[u8]> for BytesString {
563 fn as_ref(&self) -> &[u8] {
564 self.as_bytes()
565 }
566}
567
568impl AsRef<OsStr> for BytesString {
569 fn as_ref(&self) -> &OsStr {
570 OsStr::new(self.as_str())
571 }
572}
573
574impl AsRef<Path> for BytesString {
575 fn as_ref(&self) -> &Path {
576 Path::new(self.as_str())
577 }
578}
579
580impl BorrowMut<str> for BytesString {
581 fn borrow_mut(&mut self) -> &mut str {
582 self.as_mut_str()
583 }
584}
585
586impl Debug for BytesString {
587 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
588 Debug::fmt(self.as_str(), f)
589 }
590}
591
592impl Display for BytesString {
593 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594 Display::fmt(self.as_str(), f)
595 }
596}
597
598impl PartialOrd for BytesString {
599 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
600 Some(self.cmp(other))
601 }
602}
603
604impl Ord for BytesString {
605 fn cmp(&self, other: &Self) -> Ordering {
606 self.as_str().cmp(other.as_str())
607 }
608}
609
610impl<'a> Extend<&'a char> for BytesString {
611 fn extend<T: IntoIterator<Item = &'a char>>(&mut self, iter: T) {
612 self.extend(iter.into_iter().copied());
613 }
614}
615
616impl Extend<char> for BytesString {
617 fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) {
618 let mut buf = [0; 4];
619 for ch in iter {
620 let bytes = ch.encode_utf8(&mut buf);
621 self.bytes.extend_from_slice(bytes.as_bytes());
622 }
623 }
624}
625
626impl<'a> Extend<&'a str> for BytesString {
627 fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
628 for s in iter {
629 self.push_str(s);
630 }
631 }
632}
633
634impl<'a, 'b> Extend<&'a &'b str> for BytesString {
635 fn extend<T: IntoIterator<Item = &'a &'b str>>(&mut self, iter: T) {
636 for s in iter {
637 self.push_str(s);
638 }
639 }
640}
641
642impl Extend<Box<str>> for BytesString {
643 fn extend<T: IntoIterator<Item = Box<str>>>(&mut self, iter: T) {
644 for s in iter {
645 self.push_str(&s);
646 }
647 }
648}
649
650impl<'a> Extend<Cow<'a, str>> for BytesString {
651 fn extend<T: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: T) {
652 for s in iter {
653 self.push_str(&s);
654 }
655 }
656}
657
658impl Extend<String> for BytesString {
659 fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) {
660 for s in iter {
661 self.push_str(&s);
662 }
663 }
664}
665
666impl<'a> Extend<&'a String> for BytesString {
667 fn extend<T: IntoIterator<Item = &'a String>>(&mut self, iter: T) {
668 for s in iter {
669 self.push_str(s);
670 }
671 }
672}
673
674impl Extend<BytesString> for BytesString {
675 fn extend<T: IntoIterator<Item = BytesString>>(&mut self, iter: T) {
676 for s in iter {
677 self.bytes.extend(s.bytes);
678 }
679 }
680}
681
682impl FromIterator<char> for BytesString {
683 fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
684 let mut bytes = BytesString::new();
685 bytes.extend(iter);
686 bytes
687 }
688}
689
690impl<'a> FromIterator<&'a str> for BytesString {
691 fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
692 let mut bytes = BytesString::new();
693 bytes.extend(iter);
694 bytes
695 }
696}
697
698impl FromIterator<Box<str>> for BytesString {
699 fn from_iter<T: IntoIterator<Item = Box<str>>>(iter: T) -> Self {
700 let mut bytes = BytesString::new();
701 bytes.extend(iter);
702 bytes
703 }
704}
705
706impl<'a> FromIterator<Cow<'a, str>> for BytesString {
707 fn from_iter<T: IntoIterator<Item = Cow<'a, str>>>(iter: T) -> Self {
708 let mut bytes = BytesString::new();
709 bytes.extend(iter);
710 bytes
711 }
712}
713
714impl FromIterator<String> for BytesString {
715 fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
716 let mut bytes = BytesString::new();
717 bytes.extend(iter);
718 bytes
719 }
720}
721
722impl FromIterator<BytesString> for BytesString {
723 fn from_iter<T: IntoIterator<Item = BytesString>>(iter: T) -> Self {
724 let mut bytes = BytesString::new();
725 bytes.extend(iter);
726 bytes
727 }
728}
729
730impl FromStr for BytesString {
731 type Err = Infallible;
732
733 fn from_str(s: &str) -> Result<Self, Self::Err> {
734 Ok(Self {
735 bytes: BytesMut::from(s),
736 })
737 }
738}
739
740impl<I> Index<I> for BytesString
741where
742 I: SliceIndex<str>,
743{
744 type Output = I::Output;
745
746 fn index(&self, index: I) -> &Self::Output {
747 self.as_str().index(index)
748 }
749}
750
751impl<I> IndexMut<I> for BytesString
752where
753 I: SliceIndex<str>,
754{
755 fn index_mut(&mut self, index: I) -> &mut Self::Output {
756 self.as_mut_str().index_mut(index)
757 }
758}
759
760impl ToSocketAddrs for BytesString {
761 type Iter = std::vec::IntoIter<SocketAddr>;
762
763 fn to_socket_addrs(&self) -> Result<Self::Iter, std::io::Error> {
764 self.as_str().to_socket_addrs()
765 }
766}
767
768impl Hash for BytesString {
770 fn hash<H: Hasher>(&self, state: &mut H) {
771 self.as_str().hash(state);
772 }
773}
774
775impl fmt::Write for BytesString {
776 fn write_str(&mut self, s: &str) -> fmt::Result {
777 self.push_str(s);
778 Ok(())
779 }
780
781 fn write_char(&mut self, c: char) -> fmt::Result {
782 self.push(c);
783 Ok(())
784 }
785}
786
787#[cfg(feature = "serde")]
788mod serde_impl {
789 use serde::{Deserialize, Deserializer, Serialize, Serializer};
790
791 use super::*;
792
793 impl<'de> Deserialize<'de> for BytesString {
794 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
795 where
796 D: Deserializer<'de>,
797 {
798 let s = String::deserialize(deserializer)?;
799 Ok(Self::from(s))
800 }
801 }
802
803 impl Serialize for BytesString {
804 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
805 where
806 S: Serializer,
807 {
808 serializer.serialize_str(self.as_str())
809 }
810 }
811}
812
813#[cfg(test)]
814mod tests {
815 use std::{
816 collections::{hash_map::DefaultHasher, HashMap},
817 hash::{Hash, Hasher},
818 };
819
820 use super::*;
821
822 #[test]
823 fn test_new() {
824 let s = BytesString::new();
825 assert!(s.is_empty());
826 assert_eq!(s.len(), 0);
827 assert_eq!(s.as_str(), "");
828 }
829
830 #[test]
831 fn test_with_capacity() {
832 let s = BytesString::with_capacity(10);
833 assert!(s.capacity() >= 10);
834 assert!(s.is_empty());
835 assert_eq!(s.len(), 0);
836 }
837
838 #[test]
839 fn test_from_str() {
840 let s = BytesString::from("hello");
841 assert_eq!(s.as_str(), "hello");
842 assert_eq!(s.len(), 5);
843 assert!(!s.is_empty());
844 }
845
846 #[test]
847 fn test_from_string() {
848 let original = String::from("hello world");
849 let s = BytesString::from(original);
850 assert_eq!(s.as_str(), "hello world");
851 assert_eq!(s.len(), 11);
852 }
853
854 #[test]
855 fn test_from_char() {
856 let s = BytesString::from('H');
857 assert_eq!(s.as_str(), "H");
858 assert_eq!(s.len(), 1);
859
860 let s = BytesString::from('한');
862 assert_eq!(s.as_str(), "한");
863 assert_eq!(s.len(), 3); }
865
866 #[test]
867 fn test_push() {
868 let mut s = BytesString::from("hello");
869 s.push(' ');
870 s.push('w');
871 assert_eq!(s.as_str(), "hello w");
872
873 s.push('한');
875 assert_eq!(s.as_str(), "hello w한");
876 }
877
878 #[test]
879 fn test_push_str() {
880 let mut s = BytesString::from("hello");
881 s.push_str(" world");
882 assert_eq!(s.as_str(), "hello world");
883
884 s.push_str("!");
885 assert_eq!(s.as_str(), "hello world!");
886
887 s.push_str(" 한국어");
889 assert_eq!(s.as_str(), "hello world! 한국어");
890 }
891
892 #[test]
893 fn test_clear() {
894 let mut s = BytesString::from("hello");
895 assert!(!s.is_empty());
896 s.clear();
897 assert!(s.is_empty());
898 assert_eq!(s.len(), 0);
899 assert_eq!(s.as_str(), "");
900 }
901
902 #[test]
903 fn test_truncate() {
904 let mut s = BytesString::from("hello world");
905 s.truncate(5);
906 assert_eq!(s.as_str(), "hello");
907
908 s.truncate(20);
910 assert_eq!(s.as_str(), "hello");
911
912 let mut s = BytesString::from("한국어");
914 s.truncate(6); assert_eq!(s.as_str(), "한국");
916 }
917
918 #[test]
919 #[should_panic]
920 fn test_truncate_panic_on_char_boundary() {
921 let mut s = BytesString::from("한국어");
922 s.truncate(1); }
924
925 #[test]
926 fn test_split_off() {
927 let mut s = BytesString::from("hello world");
928 let other = s.split_off(6);
929 assert_eq!(s.as_str(), "hello ");
930 assert_eq!(other.as_str(), "world");
931
932 let mut s = BytesString::from("hello");
934 let other = s.split_off(0);
935 assert_eq!(s.as_str(), "");
936 assert_eq!(other.as_str(), "hello");
937
938 let mut s = BytesString::from("hello");
940 let other = s.split_off(5);
941 assert_eq!(s.as_str(), "hello");
942 assert_eq!(other.as_str(), "");
943 }
944
945 #[test]
946 fn test_as_bytes() {
947 let s = BytesString::from("hello");
948 assert_eq!(s.as_bytes(), b"hello");
949
950 let s = BytesString::from("한국어");
951 assert_eq!(s.as_bytes(), "한국어".as_bytes());
952 }
953
954 #[test]
955 fn test_as_mut_str() {
956 let mut s = BytesString::from("hello");
957 s.as_mut_str().make_ascii_uppercase();
958 assert_eq!(s.as_str(), "HELLO");
959 }
960
961 #[test]
962 fn test_into_bytes() {
963 let s = BytesString::from("hello");
964 let bytes = s.into_bytes();
965 assert_eq!(bytes.as_ref(), b"hello");
966 }
967
968 #[test]
969 fn test_from_utf8() {
970 let bytes = BytesMut::from(&b"hello"[..]);
971 let s = BytesString::from_utf8(bytes).unwrap();
972 assert_eq!(s.as_str(), "hello");
973
974 let invalid_bytes = BytesMut::from(&[0xff, 0xfe][..]);
976 assert!(BytesString::from_utf8(invalid_bytes).is_err());
977 }
978
979 #[test]
980 fn test_from_utf8_slice() {
981 let s = BytesString::from_utf8_slice(b"hello").unwrap();
982 assert_eq!(s.as_str(), "hello");
983
984 assert!(BytesString::from_utf8_slice(&[0xff, 0xfe]).is_err());
986 }
987
988 #[test]
989 fn test_from_bytes_unchecked() {
990 let bytes = BytesMut::from(&b"hello"[..]);
991 let s = unsafe { BytesString::from_bytes_unchecked(bytes) };
992 assert_eq!(s.as_str(), "hello");
993 }
994
995 #[test]
996 fn test_reserve() {
997 let mut s = BytesString::from("hello");
998 let initial_capacity = s.capacity();
999 s.reserve(100);
1000 assert!(s.capacity() >= initial_capacity + 100);
1001 assert_eq!(s.as_str(), "hello"); }
1003
1004 #[test]
1005 fn test_deref() {
1006 let s = BytesString::from("hello world");
1007 assert_eq!(s.len(), 11);
1008 assert!(s.contains("world"));
1009 assert!(s.starts_with("hello"));
1010 assert!(s.ends_with("world"));
1011 }
1012
1013 #[test]
1014 fn test_partial_eq() {
1015 let s = BytesString::from("hello");
1016
1017 assert_eq!(s, "hello");
1019 assert_ne!(s, "world");
1020
1021 assert_eq!(s, String::from("hello"));
1023 assert_ne!(s, String::from("world"));
1024
1025 assert_eq!(s, Cow::Borrowed("hello"));
1027 assert_eq!(s, Cow::Owned(String::from("hello")));
1028
1029 assert_eq!(Bytes::from("hello"), s);
1031 assert_ne!(Bytes::from("world"), s);
1032
1033 assert_eq!("hello", s);
1035 assert_eq!(String::from("hello"), s);
1036 }
1037
1038 #[test]
1039 fn test_ordering() {
1040 let s1 = BytesString::from("apple");
1041 let s2 = BytesString::from("banana");
1042 let s3 = BytesString::from("apple");
1043
1044 assert!(s1 < s2);
1045 assert!(s2 > s1);
1046 assert_eq!(s1, s3);
1047 assert!(s1 <= s3);
1048 assert!(s1 >= s3);
1049 }
1050
1051 #[test]
1052 fn test_hash() {
1053 let s1 = BytesString::from("hello");
1054 let s2 = BytesString::from("hello");
1055 let s3 = BytesString::from("world");
1056
1057 let mut hasher1 = DefaultHasher::new();
1058 let mut hasher2 = DefaultHasher::new();
1059 let mut hasher3 = DefaultHasher::new();
1060
1061 s1.hash(&mut hasher1);
1062 s2.hash(&mut hasher2);
1063 s3.hash(&mut hasher3);
1064
1065 assert_eq!(hasher1.finish(), hasher2.finish());
1066 assert_ne!(hasher1.finish(), hasher3.finish());
1067
1068 let mut str_hasher = DefaultHasher::new();
1070 "hello".hash(&mut str_hasher);
1071 assert_eq!(hasher1.finish(), str_hasher.finish());
1072 }
1073
1074 #[test]
1075 fn test_add() {
1076 let s1 = BytesString::from("hello");
1077 let s2 = s1 + " world";
1078 assert_eq!(s2.as_str(), "hello world");
1079
1080 let s3 = BytesString::from("foo");
1081 let s4 = BytesString::from("bar");
1082 let s5 = s3 + s4;
1083 assert_eq!(s5.as_str(), "foobar");
1084 }
1085
1086 #[test]
1087 fn test_add_assign() {
1088 let mut s = BytesString::from("hello");
1089 s += " world";
1090 assert_eq!(s.as_str(), "hello world");
1091
1092 let mut s1 = BytesString::from("foo");
1093 let s2 = BytesString::from("bar");
1094 s1 += s2;
1095 assert_eq!(s1.as_str(), "foobar");
1096 }
1097
1098 #[test]
1099 fn test_extend_char() {
1100 let mut s = BytesString::from("hello");
1101 s.extend(['!', ' ', '🎉'].iter());
1102 assert_eq!(s.as_str(), "hello! 🎉");
1103
1104 let mut s = BytesString::new();
1105 s.extend(['a', 'b', 'c']);
1106 assert_eq!(s.as_str(), "abc");
1107 }
1108
1109 #[test]
1110 fn test_extend_str() {
1111 let mut s = BytesString::from("hello");
1112 s.extend([" ", "world", "!"].iter());
1113 assert_eq!(s.as_str(), "hello world!");
1114
1115 let strings = vec![String::from("foo"), String::from("bar")];
1116 let mut s = BytesString::new();
1117 s.extend(&strings);
1118 assert_eq!(s.as_str(), "foobar");
1119 }
1120
1121 #[test]
1122 fn test_extend_bytes_string() {
1123 let mut s = BytesString::from("hello");
1124 let parts = vec![BytesString::from(" "), BytesString::from("world")];
1125 s.extend(parts);
1126 assert_eq!(s.as_str(), "hello world");
1127 }
1128
1129 #[test]
1130 fn test_from_iterator() {
1131 let s: BytesString = ['h', 'e', 'l', 'l', 'o'].into_iter().collect();
1132 assert_eq!(s.as_str(), "hello");
1133
1134 let s: BytesString = ["hello", " ", "world"].into_iter().collect();
1135 assert_eq!(s.as_str(), "hello world");
1136
1137 let strings = vec![String::from("foo"), String::from("bar")];
1138 let s: BytesString = strings.into_iter().collect();
1139 assert_eq!(s.as_str(), "foobar");
1140 }
1141
1142 #[test]
1143 fn test_from_str_trait() {
1144 let s: BytesString = "hello world".parse().unwrap();
1145 assert_eq!(s.as_str(), "hello world");
1146 }
1147
1148 #[test]
1149 fn test_index() {
1150 let s = BytesString::from("hello world");
1151 assert_eq!(&s[0..5], "hello");
1152 assert_eq!(&s[6..], "world");
1153 assert_eq!(&s[..5], "hello");
1154 assert_eq!(&s[6..11], "world");
1155 }
1156
1157 #[test]
1158 fn test_index_mut() {
1159 let mut s = BytesString::from("hello world");
1160 s[0..5].make_ascii_uppercase();
1161 assert_eq!(s.as_str(), "HELLO world");
1162 }
1163
1164 #[test]
1165 fn test_as_ref_implementations() {
1166 let s = BytesString::from("hello/world");
1167
1168 let str_ref: &str = s.as_ref();
1170 assert_eq!(str_ref, "hello/world");
1171
1172 let bytes_ref: &[u8] = s.as_ref();
1174 assert_eq!(bytes_ref, b"hello/world");
1175
1176 let os_str_ref: &OsStr = s.as_ref();
1178 assert_eq!(os_str_ref, OsStr::new("hello/world"));
1179
1180 let path_ref: &Path = s.as_ref();
1182 assert_eq!(path_ref, Path::new("hello/world"));
1183 }
1184
1185 #[test]
1186 fn test_borrow() {
1187 let s = BytesString::from("hello");
1188 let borrowed: &str = s.borrow();
1189 assert_eq!(borrowed, "hello");
1190
1191 let mut s = BytesString::from("hello");
1192 let borrowed_mut: &mut str = s.borrow_mut();
1193 borrowed_mut.make_ascii_uppercase();
1194 assert_eq!(s.as_str(), "HELLO");
1195 }
1196
1197 #[test]
1198 fn test_debug() {
1199 let s = BytesString::from("hello");
1200 assert_eq!(format!("{:?}", s), "\"hello\"");
1201 }
1202
1203 #[test]
1204 fn test_display() {
1205 let s = BytesString::from("hello world");
1206 assert_eq!(format!("{}", s), "hello world");
1207 }
1208
1209 #[test]
1210 fn test_conversions() {
1211 let s = BytesString::from("hello");
1212
1213 let bytes_mut: BytesMut = s.clone().into();
1215 assert_eq!(bytes_mut.as_ref(), b"hello");
1216
1217 let bytes: Bytes = s.into();
1219 assert_eq!(bytes.as_ref(), b"hello");
1220 }
1221
1222 #[test]
1223 fn test_to_socket_addrs() {
1224 let s = BytesString::from("127.0.0.1:8080");
1225 let addrs: Vec<_> = s.to_socket_addrs().unwrap().collect();
1226 assert!(!addrs.is_empty());
1227
1228 let s = BytesString::from("localhost:8080");
1229 let result = s.to_socket_addrs();
1230 let _ = result;
1233 }
1234
1235 #[test]
1236 fn test_unicode_handling() {
1237 let s = BytesString::from("Hello 🌍 한국어 🎉");
1238 assert_eq!(s.as_str(), "Hello 🌍 한국어 🎉");
1239 assert!(s.len() > 13); let mut s = BytesString::new();
1242 s.push('🌍');
1243 s.push_str(" 한국어");
1244 assert_eq!(s.as_str(), "🌍 한국어");
1245 }
1246
1247 #[test]
1248 fn test_empty_operations() {
1249 let mut s = BytesString::new();
1250 assert!(s.is_empty());
1251
1252 s.push_str("");
1253 assert!(s.is_empty());
1254
1255 s.clear();
1256 assert!(s.is_empty());
1257
1258 let other = s.split_off(0);
1259 assert!(s.is_empty());
1260 assert!(other.is_empty());
1261 }
1262
1263 #[test]
1264 fn test_large_string() {
1265 let large_str = "a".repeat(10000);
1266 let s = BytesString::from(large_str.as_str());
1267 assert_eq!(s.len(), 10000);
1268 assert_eq!(s.as_str(), large_str);
1269
1270 let mut s = BytesString::with_capacity(10000);
1271 for _ in 0..10000 {
1272 s.push('a');
1273 }
1274 assert_eq!(s.len(), 10000);
1275 assert_eq!(s.as_str(), large_str);
1276 }
1277
1278 #[test]
1279 fn test_clone() {
1280 let s1 = BytesString::from("hello world");
1281 let s2 = s1.clone();
1282 assert_eq!(s1, s2);
1283 assert_eq!(s1.as_str(), s2.as_str());
1284 }
1285
1286 #[test]
1287 fn test_default() {
1288 let s: BytesString = Default::default();
1289 assert!(s.is_empty());
1290 assert_eq!(s.as_str(), "");
1291 }
1292
1293 #[test]
1294 fn test_hash_map_usage() {
1295 let mut map = HashMap::new();
1296 let key = BytesString::from("key");
1297 map.insert(key, "value");
1298
1299 let lookup_key = BytesString::from("key");
1300 assert_eq!(map.get(&lookup_key), Some(&"value"));
1301
1302 assert_eq!(map.get("key"), Some(&"value"));
1304 }
1305}