Commit 7065544
committed
Explictly call Ord::cmp to compare priorities
Previously we would call `PartialOrd::partial_cmp` implicilty using
the overloaded comparison operators `>` and `<` when comparing
priorities, despite the fact that P is required to be `Ord`. A well
behaved implementation of `partial_cmp` is supposed to return
`Some(Ord::cmp(self, other))`. That relationship is only a convention,
so a misbehaved implementation may return None, which may cause
the order items are popped from the queue to behave seemingly randomly.
We can be a bit more defensive here and instead always call `Ord::cmp`
directly, ensuring that we never try to compare things that could
possibly return `None`. In order to enforce this going forward I added
a test that panics in the implementation for partial_cmp and exercised
all of the code paths that might call it. This isn't perfect, since
new callsites could be added, but I figure its probably good enough for
now.
Not sure exactly how to version this, it's not a breaking change if you
implementation of `PartialOrd` follows the convention, but if you don't
it would be a breaking change, since we'd be using a different
function for comparisons.1 parent 95499eb commit 7065544
File tree
4 files changed
+126
-18
lines changed- src
- double_priority_queue
- priority_queue
- tests
4 files changed
+126
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
668 | | - | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
669 | 672 | | |
670 | 673 | | |
671 | 674 | | |
| |||
705 | 708 | | |
706 | 709 | | |
707 | 710 | | |
708 | | - | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
709 | 715 | | |
710 | 716 | | |
711 | 717 | | |
| |||
901 | 907 | | |
902 | 908 | | |
903 | 909 | | |
904 | | - | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
905 | 914 | | |
906 | 915 | | |
907 | 916 | | |
908 | 917 | | |
909 | 918 | | |
910 | 919 | | |
911 | | - | |
912 | | - | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
913 | 924 | | |
914 | 925 | | |
915 | 926 | | |
| |||
943 | 954 | | |
944 | 955 | | |
945 | 956 | | |
946 | | - | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
947 | 961 | | |
948 | 962 | | |
949 | 963 | | |
950 | 964 | | |
951 | 965 | | |
952 | 966 | | |
953 | | - | |
954 | | - | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
955 | 971 | | |
956 | 972 | | |
957 | 973 | | |
| |||
970 | 986 | | |
971 | 987 | | |
972 | 988 | | |
973 | | - | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
974 | 993 | | |
975 | 994 | | |
976 | 995 | | |
| |||
1008 | 1027 | | |
1009 | 1028 | | |
1010 | 1029 | | |
1011 | | - | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
1012 | 1033 | | |
1013 | 1034 | | |
1014 | 1035 | | |
| |||
1027 | 1048 | | |
1028 | 1049 | | |
1029 | 1050 | | |
1030 | | - | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
1031 | 1054 | | |
1032 | 1055 | | |
1033 | 1056 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
542 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
543 | 546 | | |
544 | 547 | | |
545 | 548 | | |
| |||
579 | 582 | | |
580 | 583 | | |
581 | 584 | | |
582 | | - | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
583 | 589 | | |
584 | 590 | | |
585 | 591 | | |
| |||
762 | 768 | | |
763 | 769 | | |
764 | 770 | | |
765 | | - | |
| 771 | + | |
766 | 772 | | |
767 | 773 | | |
768 | 774 | | |
769 | 775 | | |
770 | | - | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
771 | 781 | | |
772 | 782 | | |
773 | 783 | | |
| |||
780 | 790 | | |
781 | 791 | | |
782 | 792 | | |
783 | | - | |
| 793 | + | |
784 | 794 | | |
785 | 795 | | |
786 | 796 | | |
787 | 797 | | |
788 | 798 | | |
789 | 799 | | |
790 | | - | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
791 | 803 | | |
792 | 804 | | |
793 | 805 | | |
| |||
802 | 814 | | |
803 | 815 | | |
804 | 816 | | |
805 | | - | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
806 | 820 | | |
807 | 821 | | |
808 | 822 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1275 | 1275 | | |
1276 | 1276 | | |
1277 | 1277 | | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
1278 | 1313 | | |
1279 | 1314 | | |
1280 | 1315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1143 | 1143 | | |
1144 | 1144 | | |
1145 | 1145 | | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
1146 | 1182 | | |
1147 | 1183 | | |
1148 | 1184 | | |
| |||
0 commit comments