Move bytes implementation into a submodule in abstractions

This commit is contained in:
Armin Ronacher 2021-02-03 14:29:24 +01:00
parent 8a6d1716ec
commit 3164c72062

View file

@ -1,6 +1,3 @@
#[cfg(feature = "bytes")]
use bstr::ByteSlice;
use std::borrow::Cow; use std::borrow::Cow;
use std::hash::Hash; use std::hash::Hash;
use std::ops::Range; use std::ops::Range;
@ -47,15 +44,6 @@ impl<'a, T: DiffableStr + ?Sized> DiffableStrRef for Cow<'a, T> {
} }
} }
#[cfg(feature = "bytes")]
impl DiffableStrRef for Vec<u8> {
type Output = [u8];
fn as_diffable_str(&self) -> &[u8] {
self.as_slice()
}
}
/// All supported diffable strings. /// All supported diffable strings.
/// ///
/// The text module can work with different types of strings depending /// The text module can work with different types of strings depending
@ -221,11 +209,24 @@ impl DiffableStr for str {
} }
} }
/// Allows viewing ASCII compatible byte slices as strings.
///
/// Requires the `bytes` feature.
#[cfg(feature = "bytes")] #[cfg(feature = "bytes")]
impl DiffableStr for [u8] { mod bytes_support {
use super::*;
use bstr::ByteSlice;
impl DiffableStrRef for Vec<u8> {
type Output = [u8];
fn as_diffable_str(&self) -> &[u8] {
self.as_slice()
}
}
/// Allows viewing ASCII compatible byte slices as strings.
///
/// Requires the `bytes` feature.
impl DiffableStr for [u8] {
fn tokenize_lines(&self) -> Vec<&Self> { fn tokenize_lines(&self) -> Vec<&Self> {
let mut iter = self.char_indices().peekable(); let mut iter = self.char_indices().peekable();
let mut last_pos = 0; let mut last_pos = 0;
@ -331,6 +332,7 @@ impl DiffableStr for [u8] {
fn as_bytes(&self) -> &[u8] { fn as_bytes(&self) -> &[u8] {
self self
} }
}
} }
#[test] #[test]