1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 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 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 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 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
#[cfg(test)]
mod tests;
use crate::borrow::{Borrow, Cow};
use crate::cmp;
use crate::collections::TryReserveError;
use crate::fmt;
use crate::hash::{Hash, Hasher};
use crate::ops;
use crate::rc::Rc;
use crate::str::FromStr;
use crate::sync::Arc;
use crate::sys::os_str::{Buf, Slice};
use crate::sys_common::{AsInner, FromInner, IntoInner};
/// 一种类型,可以表示拥有的,可变的平台原生字符串,但可以廉价地与 Rust 字符串互转换。
///
/// 对这种类型的需求源于以下事实:
///
/// * 在 Unix 系统上,字符串通常是非零字节的任意序列,在许多情况下解释为 UTF-8。
///
/// * 在 Windows 上,字符串通常是非零的 16 位值的任意序列,如果有效,则将其解释为 UTF-16。
///
/// * 在 Rust 中,字符串始终是有效的 UTF-8,其中可能包含零。
///
/// `OsString` 和 [`OsStr`] 通过同时表示 Rust 和平台原生字符串值来弥合这一差距,特别是允许 Rust 字符串在可能的情况下免费转换为 "OS" 字符串。
/// 这样的结果是 `OsString` 实例不是 * `NUL` 终止的; 为了传递给例如 Unix 系统调用,您应该创建一个 [`CStr`]。
///
/// `OsString` 到 <code>&[OsStr]</code> 如同 [`String`] 到 <code>&[str]</code>: 每对中的前一个都是拥有所有权的字符串; 后者是借用的。
///
/// 注意,`OsString` 和 [`OsStr`] 在内部不一定要以平台固有的形式保存字符串。在 Unix 上,字符串被存储为 8 位值的序列,在 Windows 上,字符串是基于 16 位值的,正如刚才所讨论的,字符串实际上也被存储为 8 位值的序列,用一种不太严格的 UTF-8 变体编码。
///
/// 这对于理解处理容量和长度值时很有用。
///
/// # `OsString` 容量
///
/// 容量对从有效 unicode 创建的 OS 字符串使用 UTF-8 字节单位,对其他内容使用未指定编码的字节单位。在给定的目标上,所有 `OsString` 和 `OsStr` 值都使用相同的容量单位,因此以下将起作用:
///
/// ```
/// use std::ffi::{OsStr, OsString};
///
/// fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
/// let mut ret = OsString::with_capacity(a.len() + b.len()); // 这将分配
/// ret.push(a); // 这不会进一步分配
/// ret.push(b); // 这不会进一步分配
/// ret
/// }
/// ```
///
/// # 创建一个 `OsString`
///
/// **来自 Rust 字符串**: `OsString` 实现了 <code>[From]<[String]></code>,因此您可以使用 <code>my_string.[into]\()</code> 从普通 Rust 字符串创建 `OsString`。
///
/// **从切片: **就像您可以从一个空的 Rust [`String`] 开始,然后用 [`String::push_str`] 将一些 <code>&[str]</code> 子字符串切片放入其中一样,您也可以使用 [`OsString::new`] 方法创建一个空的 `OsString`,然后使用 [`OsString::push`] 方法将字符串切片推入其中。
///
/// # 提取整个操作系统字符串中的借用引用
///
/// 您可以使用 [`OsString::as_os_str`] 方法从 `OsString` 获取 <code>&[OsStr]</code> ; 这实际上是对整个字符串的借用引用。
///
/// # Conversions
///
/// 请参见模块中关于 [转换][conversions] 的顶级文档,以讨论为 `OsString` from/to 原生表示形式的 [转换][conversions] 而实现的特征。
///
/// [`CStr`]: crate::ffi::CStr
/// [conversions]: super#conversions
/// [into]: Into::into
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
#[cfg_attr(not(test), rustc_diagnostic_item = "OsString")]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct OsString {
inner: Buf,
}
/// 允许在 `std` 内扩展 traits。
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OsString {}
/// 借用引用到 OS 字符串 (请参见 [`OsString`])。
///
/// 此类型表示操作系统首选表示形式中字符串的借用引用。
///
/// `&OsStr` 对 [`OsString`] 如同 <code>&[str]</code> 对 [`String`]: 每对中的前一个都是借用的引用; 后者是拥有的字符串。
///
/// 请参见模块中关于 [转换][conversions] 的顶级文档,以讨论为 `OsStr` from/to 原生表示形式的 [转换][conversions] 而实现的特征。。
///
///
/// [conversions]: super#conversions
///
///
#[cfg_attr(not(test), rustc_diagnostic_item = "OsStr")]
#[stable(feature = "rust1", since = "1.0.0")]
// FIXME:
// `OsStr::from_inner` 当前的实现依赖于与 `OsStr` 与 `Slice` 的布局兼容。
// 实现属性隐私时,应将 `OsStr` 注解为 `#[repr(transparent)]`。
// 无论如何,`OsStr` 表示形式和布局被视为实现细节,没有文档记录,因此不能依赖。
//
//
pub struct OsStr {
inner: Slice,
}
/// 允许在 `std` 内扩展 traits。
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OsStr {}
impl OsString {
/// 创建一个新的空 `OsString`。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let os_string = OsString::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn new() -> OsString {
OsString { inner: Buf::from_string(String::new()) }
}
/// 转换为 [`OsStr`] 切片。
///
/// # Examples
///
/// ```
/// use std::ffi::{OsString, OsStr};
///
/// let os_string = OsString::from("foo");
/// let os_str = OsStr::new("foo");
/// assert_eq!(os_string.as_os_str(), os_str);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
pub fn as_os_str(&self) -> &OsStr {
self
}
/// 如果 `OsString` 包含有效的 Unicode 数据,则将其转换为 [`String`]。
///
/// 失败时,将返回原始 `OsString` 的所有权。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let os_string = OsString::from("foo");
/// let string = os_string.into_string();
/// assert_eq!(string, Ok(String::from("foo")));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn into_string(self) -> Result<String, OsString> {
self.inner.into_string().map_err(|buf| OsString { inner: buf })
}
/// 用给定的 <code>&[OsStr]</code> 切片扩展字符串。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::from("foo");
/// os_string.push("bar");
/// assert_eq!(&os_string, "foobar");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
}
/// 创建一个至少具有给定容量的新 `OsString`。
///
/// 该字符串将能够保存至少 `capacity` 长度单位的其他 OS 字符串,而无需重新分配。
/// 此方法允许分配多于 `capacity` 的单元。
/// 如果 `capacity` 为 0,则不会分配该字符串。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::with_capacity(10);
/// let capacity = os_string.capacity();
///
/// // 无需重新分配即可完成此推送
/// os_string.push("foo");
///
/// assert_eq!(capacity, os_string.capacity());
/// ```
///
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn with_capacity(capacity: usize) -> OsString {
OsString { inner: Buf::with_capacity(capacity) }
}
/// 将 `OsString` 截断为零长度。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::from("foo");
/// assert_eq!(&os_string, "foo");
///
/// os_string.clear();
/// assert_eq!(&os_string, "");
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn clear(&mut self) {
self.inner.clear()
}
/// 返回此 `OsString` 无需重新分配即可容纳的容量。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let os_string = OsString::with_capacity(10);
/// assert!(os_string.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
/// 为给定的 `OsString` 插入至少至少 `additional` 的容量保留容量。
/// 如果容量已经足够,则不执行任何操作。
///
/// 集合可以保留更多空间来推测性地避免频繁的重新分配。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::new();
/// s.reserve(10);
/// assert!(s.capacity() >= 10);
/// ```
///
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
/// 尝试在给定的 `OsString` 中为至少 `additional` 多个长度单位保留容量。
/// 该字符串可能会保留更多空间来推测性地避免频繁的重新分配。
/// 调用 `try_reserve` 后,如果返回 `Ok(())`,容量将大于等于 `self.len() + additional`。
///
/// 如果容量已经足够,则不执行任何操作。即使发生错误,此方法也会保留内容。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Errors
///
/// 如果容量溢出,或者分配器报告失败,则返回错误。
///
/// # Examples
///
/// ```
/// use std::ffi::{OsStr, OsString};
/// use std::collections::TryReserveError;
///
/// fn process_data(data: &str) -> Result<OsString, TryReserveError> {
/// let mut s = OsString::new();
///
/// // 预先保留内存,如果不能,则退出
/// s.try_reserve(OsStr::new(data).len())?;
///
/// // 现在我们知道在我们复杂的工作中这不能 OOM
/// s.push(data);
///
/// Ok(s)
/// }
/// # process_data("123").expect("why is the test harness OOMing on 3 bytes?");
/// ```
///
///
#[stable(feature = "try_reserve_2", since = "1.63.0")]
#[inline]
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve(additional)
}
/// 为要插入给定 `OsString` 的至少 `additional` 更多容量保留最小容量。
///
/// 如果容量已经足够,则不执行任何操作。
///
/// 请注意,分配器可能会给集合提供比其请求更多的空间。
/// 因此,不能依靠容量来精确地最小化。
/// 如果预计将来会插入,则最好使用 [`reserve`]。
///
/// [`reserve`]: OsString::reserve
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::new();
/// s.reserve_exact(10);
/// assert!(s.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
/// 尝试在给定的 `OsString` 中为至少 `additional` 更多长度单位保留最小容量。
/// 调用 `try_reserve_exact` 后,如果返回 `Ok(())`,则容量将大于或等于 `self.len() + additional`。
///
/// 如果容量已经足够,则不执行任何操作。
///
/// 请注意,分配器可能会为 `OsString` 提供比它请求更多的空间。
/// 因此,不能依靠容量来精确地最小化。
/// 如果希望将来插入,则首选 [`try_reserve`]。
///
/// [`try_reserve`]: OsString::try_reserve
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Errors
///
/// 如果容量溢出,或者分配器报告失败,则返回错误。
///
/// # Examples
///
/// ```
/// use std::ffi::{OsStr, OsString};
/// use std::collections::TryReserveError;
///
/// fn process_data(data: &str) -> Result<OsString, TryReserveError> {
/// let mut s = OsString::new();
///
/// // 预先保留内存,如果不能,则退出
/// s.try_reserve_exact(OsStr::new(data).len())?;
///
/// // 现在我们知道在我们复杂的工作中这不能 OOM
/// s.push(data);
///
/// Ok(s)
/// }
/// # process_data("123").expect("why is the test harness OOMing on 3 bytes?");
/// ```
///
///
#[stable(feature = "try_reserve_2", since = "1.63.0")]
#[inline]
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve_exact(additional)
}
/// 缩小 `OsString` 的容量以使其长度匹配。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::from("foo");
///
/// s.reserve(100);
/// assert!(s.capacity() >= 100);
///
/// s.shrink_to_fit();
/// assert_eq!(3, s.capacity());
/// ```
#[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
#[inline]
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit()
}
/// 降低 `OsString` 的容量。
///
/// 容量将至少保持与长度和提供的值一样大。
///
///
/// 如果当前容量小于下限,则为无操作。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::from("foo");
///
/// s.reserve(100);
/// assert!(s.capacity() >= 100);
///
/// s.shrink_to(10);
/// assert!(s.capacity() >= 10);
/// s.shrink_to(0);
/// assert!(s.capacity() >= 3);
/// ```
#[inline]
#[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.inner.shrink_to(min_capacity)
}
/// 将此 `OsString` 转换为 boxed [`OsStr`]。
///
/// # Examples
///
/// ```
/// use std::ffi::{OsString, OsStr};
///
/// let s = OsString::from("hello");
///
/// let b: Box<OsStr> = s.into_boxed_os_str();
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
pub fn into_boxed_os_str(self) -> Box<OsStr> {
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl From<String> for OsString {
/// 将 [`String`] 转换为 [`OsString`]。
///
/// 此转换不会分配或复制内存。
#[inline]
fn from(s: String) -> OsString {
OsString { inner: Buf::from_string(s) }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
/// 将实现 <code>[AsRef]<[OsStr]></code> 的任何值复制到新分配的 [`OsString`] 中。
///
fn from(s: &T) -> OsString {
s.as_ref().to_os_string()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for OsString {
type Output = OsStr;
#[inline]
fn index(&self, _index: ops::RangeFull) -> &OsStr {
OsStr::from_inner(self.inner.as_slice())
}
}
#[stable(feature = "mut_osstr", since = "1.44.0")]
impl ops::IndexMut<ops::RangeFull> for OsString {
#[inline]
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
OsStr::from_inner_mut(self.inner.as_mut_slice())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for OsString {
type Target = OsStr;
#[inline]
fn deref(&self) -> &OsStr {
&self[..]
}
}
#[stable(feature = "mut_osstr", since = "1.44.0")]
impl ops::DerefMut for OsString {
#[inline]
fn deref_mut(&mut self) -> &mut OsStr {
&mut self[..]
}
}
#[stable(feature = "osstring_default", since = "1.9.0")]
impl Default for OsString {
/// 创建一个空的 `OsString`。
#[inline]
fn default() -> OsString {
OsString::new()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Clone for OsString {
#[inline]
fn clone(&self) -> Self {
OsString { inner: self.inner.clone() }
}
#[inline]
fn clone_from(&mut self, source: &Self) {
self.inner.clone_from(&source.inner)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for OsString {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, formatter)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for OsString {
#[inline]
fn eq(&self, other: &OsString) -> bool {
&**self == &**other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<str> for OsString {
#[inline]
fn eq(&self, other: &str) -> bool {
&**self == other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<OsString> for str {
#[inline]
fn eq(&self, other: &OsString) -> bool {
&**other == self
}
}
#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
impl PartialEq<&str> for OsString {
#[inline]
fn eq(&self, other: &&str) -> bool {
**self == **other
}
}
#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
impl<'a> PartialEq<OsString> for &'a str {
#[inline]
fn eq(&self, other: &OsString) -> bool {
**other == **self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for OsString {}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for OsString {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
(&**self).partial_cmp(&**other)
}
#[inline]
fn lt(&self, other: &OsString) -> bool {
&**self < &**other
}
#[inline]
fn le(&self, other: &OsString) -> bool {
&**self <= &**other
}
#[inline]
fn gt(&self, other: &OsString) -> bool {
&**self > &**other
}
#[inline]
fn ge(&self, other: &OsString) -> bool {
&**self >= &**other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd<str> for OsString {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
(&**self).partial_cmp(other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for OsString {
#[inline]
fn cmp(&self, other: &OsString) -> cmp::Ordering {
(&**self).cmp(&**other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for OsString {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(&**self).hash(state)
}
}
#[stable(feature = "os_string_fmt_write", since = "1.64.0")]
impl fmt::Write for OsString {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.push(s);
Ok(())
}
}
impl OsStr {
/// 强制转换为 `OsStr` 切片。
///
/// # Examples
///
/// ```
/// use std::ffi::OsStr;
///
/// let os_str = OsStr::new("foo");
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
s.as_ref()
}
#[inline]
fn from_inner(inner: &Slice) -> &OsStr {
// SAFETY: OsStr 只是切片的包装,因此将 &Slice 转换为 &OsStr 是安全的。
//
unsafe { &*(inner as *const Slice as *const OsStr) }
}
#[inline]
fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
// SAFETY: OsStr 只是切片的包装,因此将 &mut 切片转换为 &mut OsStr 是安全的。
// 更改 OsStr 的任何方法都必须小心,不要破坏平台特定的编码,尤其是 Windows 上的 Wtf8。
//
//
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
}
/// 如果 `OsStr` 是有效的 Unicode,则产生 <code>&[str]</code>。
///
/// 此转换可能需要检查 UTF-8 有效性。
///
/// # Examples
///
/// ```
/// use std::ffi::OsStr;
///
/// let os_str = OsStr::new("foo");
/// assert_eq!(os_str.to_str(), Some("foo"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn to_str(&self) -> Option<&str> {
self.inner.to_str()
}
/// 将 `OsStr` 转换为 <code>[Cow]<[str]></code>。
///
/// 任何非 Unicode 序列都将替换为 [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]。
///
///
/// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
///
/// # Examples
///
/// 使用无效的 Unicode 在 `OsStr` 上调用 `to_string_lossy`:
///
/// ```
/// // 注意,由于 Unix 和 Windows 表示字符串的方式不同,我们不得不使该示例复杂化,使用不同的源数据和通过不同的平台扩展来设置示例 `OsStr`。
/////
/// // 可以理解,实际上,仅通过收集用户命令行参数,您就可以得到这样的示例无效序列。
/////
/////
/////
///
/// #[cfg(unix)] {
/// use std::ffi::OsStr;
/// use std::os::unix::ffi::OsStrExt;
///
/// // 此处,值 0x66 和 0x6f 分别对应于 'f' 和 'o'。
/// // 值 0x80 是一个单独的连续字节,在 UTF-8 序列中无效。
/////
/// let source = [0x66, 0x6f, 0x80, 0x6f];
/// let os_str = OsStr::from_bytes(&source[..]);
///
/// assert_eq!(os_str.to_string_lossy(), "fo�o");
/// }
/// #[cfg(windows)] {
/// use std::ffi::OsString;
/// use std::os::windows::prelude::*;
///
/// // 在此,值 0x0066 和 0x006f 分别对应于 'f' 和 'o'。
/// // 值 0xD800 是一个单独的替代一半,在 UTF-16 序列中无效。
/////
/// let source = [0x0066, 0x006f, 0xD800, 0x006f];
/// let os_string = OsString::from_wide(&source[..]);
/// let os_str = os_string.as_os_str();
///
/// assert_eq!(os_str.to_string_lossy(), "fo�o");
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn to_string_lossy(&self) -> Cow<'_, str> {
self.inner.to_string_lossy()
}
/// 将切片复制到拥有的 [`OsString`] 中。
///
/// # Examples
///
/// ```
/// use std::ffi::{OsStr, OsString};
///
/// let os_str = OsStr::new("foo");
/// let os_string = os_str.to_os_string();
/// assert_eq!(os_string, OsString::from("foo"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn to_os_string(&self) -> OsString {
OsString { inner: self.inner.to_owned() }
}
/// 检查 `OsStr` 是否为空。
///
/// # Examples
///
/// ```
/// use std::ffi::OsStr;
///
/// let os_str = OsStr::new("");
/// assert!(os_str.is_empty());
///
/// let os_str = OsStr::new("foo");
/// assert!(!os_str.is_empty());
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.inner.is_empty()
}
/// 返回此 `OsStr` 的长度。
///
/// 请注意,这不会以 OS 字符串形式返回字符串中的字节数。
///
/// 返回的长度是 `OsStr` 使用的底层存储的长度。
/// 如 [`OsString`] 简介中所讨论的,[`OsString`] 和 `OsStr` 以最适合于原生平台和 Rust 字符串形式之间的廉价相互转换的形式存储字符串,这两种形式在存储大小和编码方面可能都大不相同。
///
///
/// 此数字对于传递给其他方法 (例如 [`OsString::with_capacity`]) 以避免重新分配非常有用。
///
/// 请参见有关编码和容量单位的主要 `OsString` 文档信息。
///
/// # Examples
///
/// ```
/// use std::ffi::OsStr;
///
/// let os_str = OsStr::new("");
/// assert_eq!(os_str.len(), 0);
///
/// let os_str = OsStr::new("foo");
/// assert_eq!(os_str.len(), 3);
/// ```
///
///
///
///
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn len(&self) -> usize {
self.inner.inner.len()
}
/// 将 <code>[Box]<[OsStr]></code> 转换为 [`OsString`],而无需复制或分配。
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_os_string(self: Box<OsStr>) -> OsString {
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
OsString { inner: Buf::from_box(boxed) }
}
/// 获取底层的字节表示形式。
///
/// Note: 至关重要的是,此 API 不应在外部公开,以避免泄露内部的,平台特定的编码。
///
#[inline]
pub(crate) fn bytes(&self) -> &[u8] {
unsafe { &*(&self.inner as *const _ as *const [u8]) }
}
/// 将此字符串就地转换为其 ASCII 小写等效项。
///
/// ASCII 字母 'A' 到 'Z' 映射到 'a' 到 'z',但是非 ASCII 字母不变。
///
/// 要返回新的小写值而不修改现有值,请使用 [`OsStr::to_ascii_lowercase`]。
///
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
///
/// s.make_ascii_lowercase();
///
/// assert_eq!("grÜße, jÜrgen ❤", s);
/// ```
///
#[stable(feature = "osstring_ascii", since = "1.53.0")]
#[inline]
pub fn make_ascii_lowercase(&mut self) {
self.inner.make_ascii_lowercase()
}
/// 将此字符串就地转换为其 ASCII 大写等效项。
///
/// ASCII 字母 'a' 到 'z' 映射到 'A' 到 'Z',但是非 ASCII 字母不变。
///
/// 要返回新的大写值而不修改现有值,请使用 [`OsStr::to_ascii_uppercase`]。
///
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut s = OsString::from("Grüße, Jürgen ❤");
///
/// s.make_ascii_uppercase();
///
/// assert_eq!("GRüßE, JüRGEN ❤", s);
/// ```
///
#[stable(feature = "osstring_ascii", since = "1.53.0")]
#[inline]
pub fn make_ascii_uppercase(&mut self) {
self.inner.make_ascii_uppercase()
}
/// 返回此字符串的副本,其中每个字符都映射为其等效的 ASCII 小写字母。
///
///
/// ASCII 字母 'A' 到 'Z' 映射到 'a' 到 'z',但是非 ASCII 字母不变。
///
/// 要就地小写该值,请使用 [`OsStr::make_ascii_lowercase`]。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
/// let s = OsString::from("Grüße, Jürgen ❤");
///
/// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
/// ```
///
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase`"]
#[stable(feature = "osstring_ascii", since = "1.53.0")]
pub fn to_ascii_lowercase(&self) -> OsString {
OsString::from_inner(self.inner.to_ascii_lowercase())
}
/// 返回此字符串的副本,其中每个字符都映射为其等效的 ASCII 大写字母。
///
///
/// ASCII 字母 'a' 到 'z' 映射到 'A' 到 'Z',但是非 ASCII 字母不变。
///
/// 要就地将值大写,请使用 [`OsStr::make_ascii_uppercase`]。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
/// let s = OsString::from("Grüße, Jürgen ❤");
///
/// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
/// ```
///
#[must_use = "to uppercase the value in-place, use `make_ascii_uppercase`"]
#[stable(feature = "osstring_ascii", since = "1.53.0")]
pub fn to_ascii_uppercase(&self) -> OsString {
OsString::from_inner(self.inner.to_ascii_uppercase())
}
/// 检查此字符串中的所有字符是否都在 ASCII 范围内。
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let ascii = OsString::from("hello!\n");
/// let non_ascii = OsString::from("Grüße, Jürgen ❤");
///
/// assert!(ascii.is_ascii());
/// assert!(!non_ascii.is_ascii());
/// ```
#[stable(feature = "osstring_ascii", since = "1.53.0")]
#[must_use]
#[inline]
pub fn is_ascii(&self) -> bool {
self.inner.is_ascii()
}
/// 检查两个字符串是否为 ASCII 不区分大小写的匹配项。
///
/// 与 `to_ascii_lowercase(a) == to_ascii_lowercase(b)` 相同,但不分配和复制临时文件。
///
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
/// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
/// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
/// ```
#[stable(feature = "osstring_ascii", since = "1.53.0")]
pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool {
self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
}
}
#[stable(feature = "box_from_os_str", since = "1.17.0")]
impl From<&OsStr> for Box<OsStr> {
/// 将字符串复制到新分配的 <code>[Box]<[OsStr]></code> 中。
#[inline]
fn from(s: &OsStr) -> Box<OsStr> {
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
#[stable(feature = "box_from_cow", since = "1.45.0")]
impl From<Cow<'_, OsStr>> for Box<OsStr> {
/// 通过复制借用的内容将 `Cow<'a, OsStr>` 转换为 <code>[Box]<[OsStr]></code>。
///
#[inline]
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
match cow {
Cow::Borrowed(s) => Box::from(s),
Cow::Owned(s) => Box::from(s),
}
}
}
#[stable(feature = "os_string_from_box", since = "1.18.0")]
impl From<Box<OsStr>> for OsString {
/// 将 <code>[Box]<[OsStr]></code> 转换为 [`OsString`],而无需复制或分配。
///
#[inline]
fn from(boxed: Box<OsStr>) -> OsString {
boxed.into_os_string()
}
}
#[stable(feature = "box_from_os_string", since = "1.20.0")]
impl From<OsString> for Box<OsStr> {
/// 将 [`OsString`] 转换为 <code>[Box]<[OsStr]></code>,而无需复制或分配。
#[inline]
fn from(s: OsString) -> Box<OsStr> {
s.into_boxed_os_str()
}
}
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
impl Clone for Box<OsStr> {
#[inline]
fn clone(&self) -> Self {
self.to_os_string().into_boxed_os_str()
}
}
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<OsString> for Arc<OsStr> {
/// 通过将 [`OsString`] 数据移动到新的 [`Arc`] 缓冲区中,将 [`OsString`] 转换为 <code>[Arc]<[OsStr]></code>。
///
#[inline]
fn from(s: OsString) -> Arc<OsStr> {
let arc = s.inner.into_arc();
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
}
}
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<&OsStr> for Arc<OsStr> {
/// 将字符串复制到新分配的 <code>[Arc]<[OsStr]></code> 中。
#[inline]
fn from(s: &OsStr) -> Arc<OsStr> {
let arc = s.inner.into_arc();
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
}
}
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<OsString> for Rc<OsStr> {
/// 通过将 [`OsString`] 数据移动到新的 [`Rc`] 缓冲区中,将 [`OsString`] 转换为 <code>[Rc]<[OsStr]></code>。
///
#[inline]
fn from(s: OsString) -> Rc<OsStr> {
let rc = s.inner.into_rc();
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
}
}
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<&OsStr> for Rc<OsStr> {
/// 将字符串复制到新分配的 <code>[Rc]<[OsStr]></code> 中。
#[inline]
fn from(s: &OsStr) -> Rc<OsStr> {
let rc = s.inner.into_rc();
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
}
}
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
impl<'a> From<OsString> for Cow<'a, OsStr> {
/// 将字符串移动到 [`Cow::Owned`] 中。
#[inline]
fn from(s: OsString) -> Cow<'a, OsStr> {
Cow::Owned(s)
}
}
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
/// 将字符串引用转换为 [`Cow::Borrowed`]。
#[inline]
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
Cow::Borrowed(s)
}
}
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
/// 将字符串引用转换为 [`Cow::Borrowed`]。
#[inline]
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
Cow::Borrowed(s.as_os_str())
}
}
#[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
impl<'a> From<Cow<'a, OsStr>> for OsString {
/// 通过复制借用的内容将 `Cow<'a, OsStr>` 转换为 [`OsString`]。
///
#[inline]
fn from(s: Cow<'a, OsStr>) -> Self {
s.into_owned()
}
}
#[stable(feature = "box_default_extra", since = "1.17.0")]
impl Default for Box<OsStr> {
#[inline]
fn default() -> Box<OsStr> {
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
#[stable(feature = "osstring_default", since = "1.9.0")]
impl Default for &OsStr {
/// 创建一个空的 `OsStr`。
#[inline]
fn default() -> Self {
OsStr::new("")
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for OsStr {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
self.bytes().eq(other.bytes())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<str> for OsStr {
#[inline]
fn eq(&self, other: &str) -> bool {
*self == *OsStr::new(other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<OsStr> for str {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
*other == *OsStr::new(self)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for OsStr {}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for OsStr {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
self.bytes().partial_cmp(other.bytes())
}
#[inline]
fn lt(&self, other: &OsStr) -> bool {
self.bytes().lt(other.bytes())
}
#[inline]
fn le(&self, other: &OsStr) -> bool {
self.bytes().le(other.bytes())
}
#[inline]
fn gt(&self, other: &OsStr) -> bool {
self.bytes().gt(other.bytes())
}
#[inline]
fn ge(&self, other: &OsStr) -> bool {
self.bytes().ge(other.bytes())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd<str> for OsStr {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
self.partial_cmp(OsStr::new(other))
}
}
// FIXME (#19470): 在我们拥有更灵活的一致性规则之前,无法为 str 提供 PartialOrd<OsStr>。
//
#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for OsStr {
#[inline]
fn cmp(&self, other: &OsStr) -> cmp::Ordering {
self.bytes().cmp(other.bytes())
}
}
macro_rules! impl_cmp {
($lhs:ty, $rhs: ty) => {
#[stable(feature = "cmp_os_str", since = "1.8.0")]
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
<OsStr as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_os_str", since = "1.8.0")]
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
<OsStr as PartialEq>::eq(self, other)
}
}
#[stable(feature = "cmp_os_str", since = "1.8.0")]
impl<'a, 'b> PartialOrd<$rhs> for $lhs {
#[inline]
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
<OsStr as PartialOrd>::partial_cmp(self, other)
}
}
#[stable(feature = "cmp_os_str", since = "1.8.0")]
impl<'a, 'b> PartialOrd<$lhs> for $rhs {
#[inline]
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
<OsStr as PartialOrd>::partial_cmp(self, other)
}
}
};
}
impl_cmp!(OsString, OsStr);
impl_cmp!(OsString, &'a OsStr);
impl_cmp!(Cow<'a, OsStr>, OsStr);
impl_cmp!(Cow<'a, OsStr>, &'b OsStr);
impl_cmp!(Cow<'a, OsStr>, OsString);
#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for OsStr {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.bytes().hash(state)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for OsStr {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, formatter)
}
}
impl OsStr {
pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, formatter)
}
}
#[unstable(feature = "slice_concat_ext", issue = "27747")]
impl<S: Borrow<OsStr>> alloc::slice::Join<&OsStr> for [S] {
type Output = OsString;
fn join(slice: &Self, sep: &OsStr) -> OsString {
let Some((first, suffix)) = slice.split_first() else {
return OsString::new();
};
let first_owned = first.borrow().to_owned();
suffix.iter().fold(first_owned, |mut a, b| {
a.push(sep);
a.push(b.borrow());
a
})
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Borrow<OsStr> for OsString {
#[inline]
fn borrow(&self) -> &OsStr {
&self[..]
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for OsStr {
type Owned = OsString;
#[inline]
fn to_owned(&self) -> OsString {
self.to_os_string()
}
#[inline]
fn clone_into(&self, target: &mut OsString) {
self.inner.clone_into(&mut target.inner)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsStr {
#[inline]
fn as_ref(&self) -> &OsStr {
self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsString {
#[inline]
fn as_ref(&self) -> &OsStr {
self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for str {
#[inline]
fn as_ref(&self) -> &OsStr {
OsStr::from_inner(Slice::from_str(self))
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for String {
#[inline]
fn as_ref(&self) -> &OsStr {
(&**self).as_ref()
}
}
impl FromInner<Buf> for OsString {
#[inline]
fn from_inner(buf: Buf) -> OsString {
OsString { inner: buf }
}
}
impl IntoInner<Buf> for OsString {
#[inline]
fn into_inner(self) -> Buf {
self.inner
}
}
impl AsInner<Slice> for OsStr {
#[inline]
fn as_inner(&self) -> &Slice {
&self.inner
}
}
#[stable(feature = "osstring_from_str", since = "1.45.0")]
impl FromStr for OsString {
type Err = core::convert::Infallible;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(OsString::from(s))
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl Extend<OsString> for OsString {
#[inline]
fn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T) {
for s in iter {
self.push(&s);
}
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> Extend<&'a OsStr> for OsString {
#[inline]
fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T) {
for s in iter {
self.push(s);
}
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> Extend<Cow<'a, OsStr>> for OsString {
#[inline]
fn extend<T: IntoIterator<Item = Cow<'a, OsStr>>>(&mut self, iter: T) {
for s in iter {
self.push(&s);
}
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl FromIterator<OsString> for OsString {
#[inline]
fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
let mut iterator = iter.into_iter();
// 因为我们正在对 `OsString`s 进行迭代,所以我们可以通过从迭代器中获取第一个字符串,并将所有后续字符串追加到该字符串来避免至少一次分配。
//
//
match iterator.next() {
None => OsString::new(),
Some(mut buf) => {
buf.extend(iterator);
buf
}
}
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> FromIterator<&'a OsStr> for OsString {
#[inline]
fn from_iter<I: IntoIterator<Item = &'a OsStr>>(iter: I) -> Self {
let mut buf = Self::new();
for s in iter {
buf.push(s);
}
buf
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> FromIterator<Cow<'a, OsStr>> for OsString {
#[inline]
fn from_iter<I: IntoIterator<Item = Cow<'a, OsStr>>>(iter: I) -> Self {
let mut iterator = iter.into_iter();
// 因为我们正在迭代 `OsString`s,所以我们可以通过从迭代器中获取第一个拥有所有权的字符串,并将所有后续字符串追加到该字符串来避免至少一次分配。
//
//
match iterator.next() {
None => OsString::new(),
Some(Cow::Owned(mut buf)) => {
buf.extend(iterator);
buf
}
Some(Cow::Borrowed(buf)) => {
let mut buf = OsString::from(buf);
buf.extend(iterator);
buf
}
}
}
}