From 8f997228baca69d4209d6f79756de65051b339d4 Mon Sep 17 00:00:00 2001 From: Caleb Jones Date: Wed, 28 Dec 2016 14:10:55 -0500 Subject: [PATCH] Make it possible to take ownership of the strings in a Sexp This lets you extend the lifetime, which is useful for things like producing error types that contain an Sexp and so on. --- Cargo.toml | 2 +- src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e14fdac..7e92b66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sexp2" -version = "0.3.0" +version = "0.3.1" authors = ["Caleb Jones "] [dependencies] diff --git a/src/lib.rs b/src/lib.rs index 25d9060..5067788 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,27 @@ impl<'a, Loc> Sexp<'a, Loc> where Loc: Span { } } +fn extend_cow<'a, T: ?Sized>(cow: &Cow<'a, T>) -> Cow<'static, T> + where T: ToOwned +{ + Cow::Owned(cow.clone().into_owned()) +} + +impl<'a, Loc> Sexp<'a, Loc> where Loc: Span + Clone { + pub fn to_owned(&self) -> Sexp<'static, Loc> { + match *self { + Sexp::Sym(ref s, ref l) => Sexp::Sym(extend_cow(s), l.clone()), + Sexp::Str(ref s, ref l) => Sexp::Str(extend_cow(s), l.clone()), + Sexp::Char(c, ref l) => Sexp::Char(c, l.clone()), + Sexp::Int(i, ref l) => Sexp::Int(i, l.clone()), + Sexp::Float(f, ref l) => Sexp::Float(f, l.clone()), + Sexp::List(ref xs, ref l) => + Sexp::List(xs.iter().map(Sexp::to_owned).collect(), + l.clone()), + } + } +} + // General Parsing Types /////////////////////////////////////////////////////// -- 2.43.2