Rust - InputSequence can be built from std::borrow::Cow too
@@ -150,12 +150,7 @@ pub enum InputSequence<'s> {
Raw(Cow<'s, str>),
PreTokenized(Cow<'s, [&'s str]>),
PreTokenizedOwned(Cow<'s, [String]>),
-}
-
-impl<'s> From<&'s str> for InputSequence<'s> {
- fn from(input: &'s str) -> Self {
- InputSequence::Raw(Cow::Borrowed(input))
- }
+ PreTokenizedCow(Cow<'s, [Cow<'s, str>]>),
}
impl<'s> From<Cow<'s, str>> for InputSequence<'s> {
@@ -164,6 +159,12 @@ impl<'s> From<Cow<'s, str>> for InputSequence<'s> {
}
}
+impl<'s> From<&'s str> for InputSequence<'s> {
+ fn from(input: &'s str) -> Self {
+ InputSequence::Raw(Cow::Borrowed(input))
+ }
+}
+
impl From<String> for InputSequence<'_> {
fn from(input: String) -> Self {
InputSequence::Raw(Cow::Owned(input))
@@ -194,6 +195,18 @@ impl<'s> From<Vec<String>> for InputSequence<'s> {
}
}
+impl<'s> From<Vec<Cow<'s, str>>> for InputSequence<'s> {
+ fn from(input: Vec<Cow<'s, str>>) -> Self {
+ InputSequence::PreTokenizedCow(Cow::Owned(input))
+ }
+}
+
+impl<'s> From<&'s [Cow<'s, str>]> for InputSequence<'s> {
+ fn from(input: &'s [Cow<'s, str>]) -> Self {
+ InputSequence::PreTokenizedCow(Cow::Borrowed(input))
+ }
+}
+
#[derive(Debug, Clone)]
pub enum EncodeInput<'s> {
Single(InputSequence<'s>),
@@ -617,6 +630,11 @@ where
.enumerate()
.map(|(i, sequence)| encode(true, i, sequence))
.collect(),
+ InputSequence::PreTokenizedCow(seq) => seq
+ .iter()
+ .enumerate()
+ .map(|(i, sequence)| encode(true, i, sequence))
+ .collect(),
InputSequence::Raw(seq) => encode(false, 0, seq.as_ref()),
}
}
