comparison rust/hg-core/src/utils/hg_path.rs @ 49930:e98fd81bb151

rust-clippy: fix most warnings in `hg-core` All of these are simple changes that for the most part are clear improvements and the rest are at most equivalent. The remaining warnings have to be fixed either with a bigger refactor like for the nested "revlog" module, or in the dependency `bytes-cast`, which we own. This will be done sometime in the future.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 09 Jan 2023 19:18:43 +0100
parents c7fb9b74e753
children 331a3cbe1c9e
comparison
equal deleted inserted replaced
49929:5f1cd6839c69 49930:e98fd81bb151
203 /// let path = HgPath::new(b"pathwithoutsep").split_filename(); 203 /// let path = HgPath::new(b"pathwithoutsep").split_filename();
204 /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep"))); 204 /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep")));
205 /// ``` 205 /// ```
206 pub fn split_filename(&self) -> (&Self, &Self) { 206 pub fn split_filename(&self) -> (&Self, &Self) {
207 match &self.inner.iter().rposition(|c| *c == b'/') { 207 match &self.inner.iter().rposition(|c| *c == b'/') {
208 None => (HgPath::new(""), &self), 208 None => (HgPath::new(""), self),
209 Some(size) => ( 209 Some(size) => (
210 HgPath::new(&self.inner[..*size]), 210 HgPath::new(&self.inner[..*size]),
211 HgPath::new(&self.inner[*size + 1..]), 211 HgPath::new(&self.inner[*size + 1..]),
212 ), 212 ),
213 } 213 }
324 } 324 }
325 325
326 #[cfg(unix)] 326 #[cfg(unix)]
327 /// Split a pathname into drive and path. On Posix, drive is always empty. 327 /// Split a pathname into drive and path. On Posix, drive is always empty.
328 pub fn split_drive(&self) -> (&HgPath, &HgPath) { 328 pub fn split_drive(&self) -> (&HgPath, &HgPath) {
329 (HgPath::new(b""), &self) 329 (HgPath::new(b""), self)
330 } 330 }
331 331
332 /// Checks for errors in the path, short-circuiting at the first one. 332 /// Checks for errors in the path, short-circuiting at the first one.
333 /// This generates fine-grained errors useful for debugging. 333 /// This generates fine-grained errors useful for debugging.
334 /// To simply check if the path is valid during tests, use `is_valid`. 334 /// To simply check if the path is valid during tests, use `is_valid`.
394 impl HgPathBuf { 394 impl HgPathBuf {
395 pub fn new() -> Self { 395 pub fn new() -> Self {
396 Default::default() 396 Default::default()
397 } 397 }
398 398
399 pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) -> () { 399 pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) {
400 if !self.inner.is_empty() && self.inner.last() != Some(&b'/') { 400 if !self.inner.is_empty() && self.inner.last() != Some(&b'/') {
401 self.inner.push(b'/'); 401 self.inner.push(b'/');
402 } 402 }
403 self.inner.extend(other.as_ref().bytes()) 403 self.inner.extend(other.as_ref().bytes())
404 } 404 }
429 impl Deref for HgPathBuf { 429 impl Deref for HgPathBuf {
430 type Target = HgPath; 430 type Target = HgPath;
431 431
432 #[inline] 432 #[inline]
433 fn deref(&self) -> &HgPath { 433 fn deref(&self) -> &HgPath {
434 &HgPath::new(&self.inner) 434 HgPath::new(&self.inner)
435 } 435 }
436 } 436 }
437 437
438 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf { 438 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
439 fn from(s: &T) -> HgPathBuf { 439 fn from(s: &T) -> HgPathBuf {
440 s.as_ref().to_owned() 440 s.as_ref().to_owned()
441 } 441 }
442 } 442 }
443 443
444 impl Into<Vec<u8>> for HgPathBuf { 444 impl From<HgPathBuf> for Vec<u8> {
445 fn into(self) -> Vec<u8> { 445 fn from(val: HgPathBuf) -> Self {
446 self.inner 446 val.inner
447 } 447 }
448 } 448 }
449 449
450 impl Borrow<HgPath> for HgPathBuf { 450 impl Borrow<HgPath> for HgPathBuf {
451 fn borrow(&self) -> &HgPath { 451 fn borrow(&self) -> &HgPath {
452 &HgPath::new(self.as_bytes()) 452 HgPath::new(self.as_bytes())
453 } 453 }
454 } 454 }
455 455
456 impl ToOwned for HgPath { 456 impl ToOwned for HgPath {
457 type Owned = HgPathBuf; 457 type Owned = HgPathBuf;
489 hg_path.as_ref().check_state()?; 489 hg_path.as_ref().check_state()?;
490 let os_str; 490 let os_str;
491 #[cfg(unix)] 491 #[cfg(unix)]
492 { 492 {
493 use std::os::unix::ffi::OsStrExt; 493 use std::os::unix::ffi::OsStrExt;
494 os_str = std::ffi::OsStr::from_bytes(&hg_path.as_ref().as_bytes()); 494 os_str = std::ffi::OsStr::from_bytes(hg_path.as_ref().as_bytes());
495 } 495 }
496 // TODO Handle other platforms 496 // TODO Handle other platforms
497 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding). 497 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
498 Ok(os_str.to_os_string()) 498 Ok(os_str.to_os_string())
499 } 499 }
509 ) -> Result<HgPathBuf, HgPathError> { 509 ) -> Result<HgPathBuf, HgPathError> {
510 let buf; 510 let buf;
511 #[cfg(unix)] 511 #[cfg(unix)]
512 { 512 {
513 use std::os::unix::ffi::OsStrExt; 513 use std::os::unix::ffi::OsStrExt;
514 buf = HgPathBuf::from_bytes(&os_string.as_ref().as_bytes()); 514 buf = HgPathBuf::from_bytes(os_string.as_ref().as_bytes());
515 } 515 }
516 // TODO Handle other platforms 516 // TODO Handle other platforms
517 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding). 517 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
518 518
519 buf.check_state()?; 519 buf.check_state()?;
526 let buf; 526 let buf;
527 let os_str = path.as_ref().as_os_str(); 527 let os_str = path.as_ref().as_os_str();
528 #[cfg(unix)] 528 #[cfg(unix)]
529 { 529 {
530 use std::os::unix::ffi::OsStrExt; 530 use std::os::unix::ffi::OsStrExt;
531 buf = HgPathBuf::from_bytes(&os_str.as_bytes()); 531 buf = HgPathBuf::from_bytes(os_str.as_bytes());
532 } 532 }
533 // TODO Handle other platforms 533 // TODO Handle other platforms
534 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding). 534 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
535 535
536 buf.check_state()?; 536 buf.check_state()?;