chiark / gitweb /
subst: Have internal functions take borrows
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 10:28:30 +0000 (11:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 May 2022 13:24:11 +0000 (14:24 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/shapelib.rs

index 5aec88e97c25cae165d48ef7a38bf372d80c15c9..3d3ce64642351ab91a0a5a170559b7fc5eb4ab48 100644 (file)
@@ -1056,7 +1056,7 @@ impl<'s> Substituting<'s> {
 }
 
 #[throws(SubstError)]
-fn subst_general<'i>(input: Substituting<'i>,
+fn subst_general_mf1<'i>(input: &Substituting<'i>,
                  needle: &'static str, replacement: &str)
                  -> (Substituting<'i>, usize) {
   let mut count = 0;
@@ -1081,13 +1081,23 @@ fn subst_general<'i>(input: Substituting<'i>,
   (Substituting{ s: work.into(), mformat: input.mformat }, count)
 }
 
+#[throws(SubstError)]
+// This takes &Substituting.  The rest of the code uses subst or
+// substn, which takes Substituting, thus ensuring that at some future
+// time we might be able to accumulate all the substitutions in
+// Substituting and do them all at once.
+fn subst_general<'i>(input: &Substituting<'i>,
+                 needle: &'static str, replacement: &str)
+                 -> (Substituting<'i>, usize) {
+  subst_general_mf1(input, needle, replacement)?
+}
+
 #[throws(SubstError)]
 fn subst<'i>(before: Substituting<'i>, needle: &'static str, replacement: &str)
          -> Substituting<'i> {
   use SubstErrorKind as SEK;
-  let err_s = (*before.s).to_owned(); // todo: avoid this
-  let err = |kind| SubstError { kind, input: err_s };
-  let (out, count) = subst_general(before, needle, replacement)?;
+  let err = |kind| SubstError { kind, input: (*before.s).to_owned() };
+  let (out, count) = subst_general(&before, needle, replacement)?;
   if count == 0 { throw!(err(SEK::MissingToken(needle))) }
   if count > 1 { throw!(err(SEK::RepeatedToken(needle))) }
   out
@@ -1096,7 +1106,7 @@ fn subst<'i>(before: Substituting<'i>, needle: &'static str, replacement: &str)
 #[throws(SubstError)]
 fn substn<'i>(before: Substituting<'i>, needle: &'static str, replacement: &str)
           -> Substituting<'i> {
-  subst_general(before, needle, replacement)?.0
+  subst_general(&before, needle, replacement)?.0
 }
 
 /*