##// END OF EJS Templates
rawdata: update caller in remotefilelog...
rawdata: update caller in remotefilelog We update callers incrementally because this help bisecting failures. This was useful during development, so we expect it might be useful again in the future.

File last commit:

r42978:0def0fcb default
r43016:d53975bb default
Show More
utils.rs
101 lines | 2.3 KiB | application/rls-services+xml | RustLexer
Raphaël Gomès
rust-docstrings: add missing module docstrings...
r42996 // utils module
//
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Contains useful functions, traits, structs, etc. for use in core.
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 pub mod files;
Raphaël Gomès
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop...
r42993 use std::convert::AsMut;
/// Takes a slice and copies it into an array.
///
/// # Panics
///
/// Will panic if the slice and target array don't have the same length.
pub fn copy_into_array<A, T>(slice: &[T]) -> A
where
A: Sized + Default + AsMut<[T]>,
T: Copy,
{
let mut a = Default::default();
<A as AsMut<[T]>>::as_mut(&mut a).copy_from_slice(slice);
a
}
Raphaël Gomès
rust-utils: add docstrings and doctests for utils.rs...
r42829 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
///
/// # Examples
///
/// ```
/// use crate::hg::utils::replace_slice;
/// let mut line = b"I hate writing tests!".to_vec();
/// replace_slice(&mut line, b"hate", b"love");
/// assert_eq!(
/// line,
/// b"I love writing tests!".to_vec()
///);
///
/// ```
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 pub fn replace_slice<T>(buf: &mut [T], from: &[T], to: &[T])
where
T: Clone + PartialEq,
{
Raphaël Gomès
rust-utils: remove buggy assertion...
r42830 if buf.len() < from.len() || from.len() != to.len() {
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 return;
}
for i in 0..=buf.len() - from.len() {
if buf[i..].starts_with(from) {
buf[i..(i + from.len())].clone_from_slice(to);
}
}
}
pub trait SliceExt {
Raphaël Gomès
rust-utils: add docstrings and doctests for utils.rs...
r42829 fn trim_end(&self) -> &Self;
fn trim_start(&self) -> &Self;
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 fn trim(&self) -> &Self;
}
fn is_not_whitespace(c: &u8) -> bool {
!(*c as char).is_whitespace()
}
impl SliceExt for [u8] {
fn trim_end(&self) -> &[u8] {
if let Some(last) = self.iter().rposition(is_not_whitespace) {
&self[..last + 1]
} else {
&[]
}
}
Raphaël Gomès
rust-utils: add docstrings and doctests for utils.rs...
r42829 fn trim_start(&self) -> &[u8] {
if let Some(first) = self.iter().position(is_not_whitespace) {
&self[first..]
} else {
&[]
}
}
/// ```
/// use hg::utils::SliceExt;
/// assert_eq!(
/// b" to trim ".trim(),
/// b"to trim"
/// );
/// assert_eq!(
/// b"to trim ".trim(),
/// b"to trim"
/// );
/// assert_eq!(
/// b" to trim".trim(),
/// b"to trim"
/// );
/// ```
fn trim(&self) -> &[u8] {
self.trim_start().trim_end()
}
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 }