Skip to content

Commit

Permalink
Implement more shims that rely on Pattern trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed May 5, 2021
1 parent f3e37b7 commit 0f63341
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(rust_2018_idioms, unused_qualifications)]
#![allow(
non_camel_case_types,
unstable_name_collisions,
clippy::missing_safety_doc,
clippy::wrong_self_convention,
clippy::match_like_matches_macro
)]
#![allow(non_camel_case_types, unstable_name_collisions, clippy::all)]

/*!
Standback backports a number of methods, structs, and macros that have been stabilized in the Rust
Expand Down Expand Up @@ -86,6 +80,7 @@ slice::split_inclusive_mut
slice::split_inclusive
slice::strip_prefix
slice::strip_suffix
str::split_inclusive
task::Wake
i*::unsigned_abs
Poll::map_ok
Expand Down Expand Up @@ -144,6 +139,8 @@ Option::zip
```text
i*::saturating_abs
i*::saturating_neg
str::strip_prefix
str::strip_suffix
```
## 1.44
Expand Down Expand Up @@ -364,7 +361,7 @@ pub mod prelude {
#[cfg(all(shim = "1.44", feature = "std"))]
pub use crate::v1_44::PathBuf_v1_44;
#[cfg(shim = "1.45")]
pub use crate::v1_45::int_v1_45;
pub use crate::v1_45::{int_v1_45, str_v1_45};
#[cfg(shim = "1.46")]
pub use crate::v1_46::{int_v1_46, Option_v1_46};
#[cfg(all(shim = "1.47", feature = "alloc"))]
Expand All @@ -388,7 +385,9 @@ pub mod prelude {
#[cfg(all(shim = "1.51", feature = "std"))]
pub use crate::v1_51::Seek_v1_51;
#[cfg(shim = "1.51")]
pub use crate::v1_51::{Peekable_v1_51, Poll_v1_51, SignedInteger_v1_51, Slice_v1_51};
pub use crate::v1_51::{
str_v1_51, Peekable_v1_51, Poll_v1_51, SignedInteger_v1_51, Slice_v1_51,
};
#[cfg(shim = "1.52")]
pub use crate::v1_52::{char_v1_52, str_v1_52, Slice_v1_52};
}
Expand Down
29 changes: 28 additions & 1 deletion src/v1_45.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::traits::SignedInteger;
use crate::pattern::{Pattern, ReverseSearcher};
use crate::traits::{Sealed, SignedInteger};

pub trait int_v1_45: SignedInteger {
fn saturating_neg(self) -> Self;
Expand Down Expand Up @@ -28,3 +29,29 @@ macro_rules! impl_int_v1_45 {
}

impl_int_v1_45![i8, i16, i32, i64, i128, isize];

pub trait str_v1_45: Sealed<str> {
fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str>;
fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>;
}

impl str_v1_45 for str {
#[must_use = "this returns the remaining substring as a new slice, without modifying the \
original"]
fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
prefix.strip_prefix_of(self)
}

#[must_use = "this returns the remaining substring as a new slice, without modifying the \
original"]
fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
{
suffix.strip_suffix_of(self)
}
}
19 changes: 19 additions & 0 deletions src/v1_51.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod slice;
mod str;

#[cfg(feature = "alloc")]
use alloc::sync::Arc;
Expand All @@ -9,6 +10,7 @@ use core::task::Poll;
#[cfg(feature = "std")]
use std::io::{Seek, SeekFrom};

use crate::pattern::Pattern;
use crate::traits::Sealed;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -210,3 +212,20 @@ impl<T, E> Poll_v1_51<T, E> for Poll<Result<T, E>> {
}
}
}

pub trait str_v1_51: Sealed<str> {
fn split_inclusive<'a, P: Pattern<'a>>(&'a self, pat: P) -> str::SplitInclusive<'a, P>;
}

impl str_v1_51 for str {
#[inline]
fn split_inclusive<'a, P: Pattern<'a>>(&'a self, pat: P) -> str::SplitInclusive<'a, P> {
str::SplitInclusive(str::SplitInternal {
start: 0,
end: self.len(),
matcher: pat.into_searcher(self),
allow_trailing_empty: false,
finished: false,
})
}
}
29 changes: 29 additions & 0 deletions src/v1_51/str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use core::fmt;

use crate::pattern::Pattern;

pub struct SplitInclusive<'a, P: Pattern<'a>>(pub(super) SplitInternal<'a, P>);

pub(super) struct SplitInternal<'a, P: Pattern<'a>> {
pub(super) start: usize,
pub(super) end: usize,
pub(super) matcher: P::Searcher,
pub(super) allow_trailing_empty: bool,
pub(super) finished: bool,
}

impl<'a, P> fmt::Debug for SplitInternal<'a, P>
where
P: Pattern<'a>,
P::Searcher: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SplitInternal")
.field("start", &self.start)
.field("end", &self.end)
.field("matcher", &self.matcher)
.field("allow_trailing_empty", &self.allow_trailing_empty)
.field("finished", &self.finished)
.finish()
}
}

0 comments on commit 0f63341

Please sign in to comment.