@@ -144,16 +144,29 @@ impl GStr {
144
144
/// Wraps a raw C string with a safe GLib string wrapper. The provided C string **must** be
145
145
/// nul-terminated. All constraints from [`std::ffi::CStr::from_ptr`] also apply here.
146
146
///
147
- /// If the string is valid UTF-8 then it is directly returned otherwise a copy is created with
148
- /// every invalid character replaced by the Unicode replacement character (U+FFFD).
147
+ /// If the string is valid UTF-8 then it is directly returned, otherwise `None` is returned.
149
148
#[ inline]
150
- pub unsafe fn from_ptr_lossy < ' a > ( ptr : * const c_char ) -> Cow < ' a , Self > {
149
+ pub unsafe fn from_ptr_checked < ' a > ( ptr : * const c_char ) -> Option < & ' a Self > {
151
150
let mut end_ptr = ptr:: null ( ) ;
152
151
if ffi:: g_utf8_validate ( ptr as * const _ , -1 , & mut end_ptr) != ffi:: GFALSE {
153
- Cow :: Borrowed ( Self :: from_utf8_with_nul_unchecked ( slice:: from_raw_parts (
152
+ Some ( Self :: from_utf8_with_nul_unchecked ( slice:: from_raw_parts (
154
153
ptr as * const u8 ,
155
154
end_ptr. offset_from ( ptr) as usize + 1 ,
156
155
) ) )
156
+ } else {
157
+ None
158
+ }
159
+ }
160
+ // rustdoc-stripper-ignore-next
161
+ /// Wraps a raw C string with a safe GLib string wrapper. The provided C string **must** be
162
+ /// nul-terminated. All constraints from [`std::ffi::CStr::from_ptr`] also apply here.
163
+ ///
164
+ /// If the string is valid UTF-8 then it is directly returned otherwise a copy is created with
165
+ /// every invalid character replaced by the Unicode replacement character (U+FFFD).
166
+ #[ inline]
167
+ pub unsafe fn from_ptr_lossy < ' a > ( ptr : * const c_char ) -> Cow < ' a , Self > {
168
+ if let Some ( gs) = Self :: from_ptr_checked ( ptr) {
169
+ Cow :: Borrowed ( gs)
157
170
} else {
158
171
Cow :: Owned ( GString :: from_glib_full ( ffi:: g_utf8_make_valid (
159
172
ptr as * const _ ,
@@ -684,6 +697,24 @@ impl GStringPtr {
684
697
pub fn to_str ( & self ) -> & str {
685
698
self . to_gstr ( ) . as_str ( )
686
699
}
700
+
701
+ // rustdoc-stripper-ignore-next
702
+ /// Returns the string's C pointer.
703
+ #[ inline]
704
+ pub const fn as_ptr ( & self ) -> * const c_char {
705
+ self . 0 . as_ptr ( )
706
+ }
707
+
708
+ // rustdoc-stripper-ignore-next
709
+ /// Wrapper around `libc::strcmp` returning `Ordering`.
710
+ ///
711
+ /// # Safety
712
+ ///
713
+ /// `a` and `b` must be non-null pointers to nul-terminated C strings.
714
+ #[ inline]
715
+ unsafe fn strcmp ( a : * const c_char , b : * const c_char ) -> Ordering {
716
+ from_glib ( libc:: strcmp ( a, b) )
717
+ }
687
718
}
688
719
689
720
impl Clone for GStringPtr {
@@ -693,6 +724,13 @@ impl Clone for GStringPtr {
693
724
}
694
725
}
695
726
727
+ impl IntoGlibPtr < * mut c_char > for GStringPtr {
728
+ #[ inline]
729
+ unsafe fn into_glib_ptr ( self ) -> * mut c_char {
730
+ self . 0 . as_ptr ( )
731
+ }
732
+ }
733
+
696
734
impl Drop for GStringPtr {
697
735
#[ inline]
698
736
fn drop ( & mut self ) {
@@ -704,7 +742,7 @@ impl Drop for GStringPtr {
704
742
705
743
impl fmt:: Debug for GStringPtr {
706
744
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
707
- f . write_str ( self . to_str ( ) )
745
+ < & GStr as fmt :: Debug > :: fmt ( & self . to_gstr ( ) , f )
708
746
}
709
747
}
710
748
@@ -720,105 +758,105 @@ impl Eq for GStringPtr {}
720
758
impl PartialEq for GStringPtr {
721
759
#[ inline]
722
760
fn eq ( & self , other : & GStringPtr ) -> bool {
723
- self . to_gstr ( ) == other . to_gstr ( )
761
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
724
762
}
725
763
}
726
764
727
765
impl PartialEq < GStringPtr > for String {
728
766
#[ inline]
729
767
fn eq ( & self , other : & GStringPtr ) -> bool {
730
- self . as_str ( ) == other . to_str ( )
768
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
731
769
}
732
770
}
733
771
734
772
impl PartialEq < GStringPtr > for GString {
735
773
#[ inline]
736
774
fn eq ( & self , other : & GStringPtr ) -> bool {
737
- self . as_str ( ) == other . to_str ( )
775
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
738
776
}
739
777
}
740
778
741
779
impl PartialEq < str > for GStringPtr {
742
780
#[ inline]
743
781
fn eq ( & self , other : & str ) -> bool {
744
- self . to_str ( ) == other
782
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
745
783
}
746
784
}
747
785
748
786
impl PartialEq < & str > for GStringPtr {
749
787
#[ inline]
750
788
fn eq ( & self , other : & & str ) -> bool {
751
- self . to_str ( ) == * other
789
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
752
790
}
753
791
}
754
792
755
793
impl PartialEq < GStr > for GStringPtr {
756
794
#[ inline]
757
795
fn eq ( & self , other : & GStr ) -> bool {
758
- self . to_gstr ( ) == other
796
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
759
797
}
760
798
}
761
799
762
800
impl PartialEq < & GStr > for GStringPtr {
763
801
#[ inline]
764
802
fn eq ( & self , other : & & GStr ) -> bool {
765
- self . to_gstr ( ) == * other
803
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
766
804
}
767
805
}
768
806
769
807
impl PartialEq < GStringPtr > for & str {
770
808
#[ inline]
771
809
fn eq ( & self , other : & GStringPtr ) -> bool {
772
- * self == other . to_str ( )
810
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
773
811
}
774
812
}
775
813
776
814
impl PartialEq < GStringPtr > for & GStr {
777
815
#[ inline]
778
816
fn eq ( & self , other : & GStringPtr ) -> bool {
779
- self . as_str ( ) == other . to_str ( )
817
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
780
818
}
781
819
}
782
820
783
821
impl PartialEq < String > for GStringPtr {
784
822
#[ inline]
785
823
fn eq ( & self , other : & String ) -> bool {
786
- self . to_str ( ) == other . as_str ( )
824
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
787
825
}
788
826
}
789
827
790
828
impl PartialEq < GString > for GStringPtr {
791
829
#[ inline]
792
830
fn eq ( & self , other : & GString ) -> bool {
793
- self . to_str ( ) == other . as_str ( )
831
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
794
832
}
795
833
}
796
834
797
835
impl PartialEq < GStringPtr > for str {
798
836
#[ inline]
799
837
fn eq ( & self , other : & GStringPtr ) -> bool {
800
- self == other . to_str ( )
838
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
801
839
}
802
840
}
803
841
804
842
impl PartialEq < GStringPtr > for GStr {
805
843
#[ inline]
806
844
fn eq ( & self , other : & GStringPtr ) -> bool {
807
- self == other . to_gstr ( )
845
+ self . partial_cmp ( other ) == Some ( Ordering :: Equal )
808
846
}
809
847
}
810
848
811
849
impl PartialOrd < GStringPtr > for GStringPtr {
812
850
#[ inline]
813
851
fn partial_cmp ( & self , other : & GStringPtr ) -> Option < std:: cmp:: Ordering > {
814
- Some ( self . to_gstr ( ) . cmp ( other. to_gstr ( ) ) )
852
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
815
853
}
816
854
}
817
855
818
856
impl Ord for GStringPtr {
819
857
#[ inline]
820
858
fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
821
- self . to_gstr ( ) . cmp ( other. to_gstr ( ) )
859
+ unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) }
822
860
}
823
861
}
824
862
@@ -832,21 +870,21 @@ impl PartialOrd<GStringPtr> for String {
832
870
impl PartialOrd < GStringPtr > for GString {
833
871
#[ inline]
834
872
fn partial_cmp ( & self , other : & GStringPtr ) -> Option < std:: cmp:: Ordering > {
835
- Some ( self . as_str ( ) . cmp ( other. to_str ( ) ) )
873
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
836
874
}
837
875
}
838
876
839
877
impl PartialOrd < String > for GStringPtr {
840
878
#[ inline]
841
879
fn partial_cmp ( & self , other : & String ) -> Option < std:: cmp:: Ordering > {
842
- Some ( self . to_str ( ) . cmp ( other. as_str ( ) ) )
880
+ Some ( self . to_str ( ) . cmp ( other) )
843
881
}
844
882
}
845
883
846
884
impl PartialOrd < GString > for GStringPtr {
847
885
#[ inline]
848
886
fn partial_cmp ( & self , other : & GString ) -> Option < std:: cmp:: Ordering > {
849
- Some ( self . to_str ( ) . cmp ( other. as_str ( ) ) )
887
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
850
888
}
851
889
}
852
890
@@ -860,7 +898,7 @@ impl PartialOrd<GStringPtr> for str {
860
898
impl PartialOrd < GStringPtr > for GStr {
861
899
#[ inline]
862
900
fn partial_cmp ( & self , other : & GStringPtr ) -> Option < std:: cmp:: Ordering > {
863
- Some ( self . cmp ( other. to_gstr ( ) ) )
901
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
864
902
}
865
903
}
866
904
@@ -871,10 +909,38 @@ impl PartialOrd<str> for GStringPtr {
871
909
}
872
910
}
873
911
912
+ impl PartialOrd < & str > for GStringPtr {
913
+ #[ inline]
914
+ fn partial_cmp ( & self , other : & & str ) -> Option < std:: cmp:: Ordering > {
915
+ Some ( self . to_str ( ) . cmp ( other) )
916
+ }
917
+ }
918
+
874
919
impl PartialOrd < GStr > for GStringPtr {
875
920
#[ inline]
876
921
fn partial_cmp ( & self , other : & GStr ) -> Option < std:: cmp:: Ordering > {
877
- Some ( self . to_gstr ( ) . cmp ( other) )
922
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
923
+ }
924
+ }
925
+
926
+ impl PartialOrd < & GStr > for GStringPtr {
927
+ #[ inline]
928
+ fn partial_cmp ( & self , other : & & GStr ) -> Option < std:: cmp:: Ordering > {
929
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
930
+ }
931
+ }
932
+
933
+ impl PartialOrd < GStringPtr > for & str {
934
+ #[ inline]
935
+ fn partial_cmp ( & self , other : & GStringPtr ) -> Option < std:: cmp:: Ordering > {
936
+ Some ( self . cmp ( & other. to_str ( ) ) )
937
+ }
938
+ }
939
+
940
+ impl PartialOrd < GStringPtr > for & GStr {
941
+ #[ inline]
942
+ fn partial_cmp ( & self , other : & GStringPtr ) -> Option < std:: cmp:: Ordering > {
943
+ Some ( unsafe { GStringPtr :: strcmp ( self . as_ptr ( ) , other. as_ptr ( ) ) } )
878
944
}
879
945
}
880
946
0 commit comments