]> Repositorios git - scryer-prolog.git/commitdiff
Use Cow<'a,B> for toplevel configuration
authorbakaq <[email protected]>
Thu, 12 Dec 2024 08:59:59 +0000 (05:59 -0300)
committerbakaq <[email protected]>
Thu, 12 Dec 2024 08:59:59 +0000 (05:59 -0300)
src/machine/config.rs
src/machine/mod.rs

index 4e1e053201e9cb4cb7108a6cac22a85869e879bd..2981899d0899e2461a517eb7db013e01d5c8f97c 100644 (file)
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
 use rand::{rngs::StdRng, SeedableRng};
 
 use crate::Machine;
@@ -41,7 +43,7 @@ enum StreamConfigInner {
 /// Describes how a [`Machine`](crate::Machine) will be configured.
 pub struct MachineBuilder {
     pub(crate) streams: StreamConfig,
-    pub(crate) toplevel: &'static str,
+    pub(crate) toplevel: Cow<'static, str>,
 }
 
 impl Default for MachineBuilder {
@@ -49,7 +51,7 @@ impl Default for MachineBuilder {
     fn default() -> Self {
         MachineBuilder {
             streams: Default::default(),
-            toplevel: default_toplevel(),
+            toplevel: default_toplevel().into(),
         }
     }
 }
@@ -67,8 +69,8 @@ impl MachineBuilder {
     }
 
     /// Uses the given toplevel in this configuration.
-    pub fn with_toplevel(mut self, toplevel: &'static str) -> Self {
-        self.toplevel = toplevel;
+    pub fn with_toplevel(mut self, toplevel: impl Into<Cow<'static, str>>) -> Self {
+        self.toplevel = toplevel.into();
         self
     }
 
index 22c2a0322d7480b56a8df38cd082637f37866906..a62c0caa8e79fc6db41d9b9a5bf7e5526800a809 100644 (file)
@@ -54,6 +54,7 @@ use lazy_static::lazy_static;
 use ordered_float::OrderedFloat;
 
 use rand::rngs::StdRng;
+use std::borrow::Cow;
 use std::cmp::Ordering;
 use std::env;
 use std::io::Read;
@@ -297,13 +298,16 @@ impl Machine {
         self.run_module_predicate(atom!("loader"), (atom!("file_load"), 2));
     }
 
-    fn load_top_level(&mut self, program: &'static str) {
+    fn load_top_level(&mut self, program: Cow<'static, str>) {
         let mut path_buf = current_dir();
 
         path_buf.push("src/toplevel.pl");
 
         let path = path_buf.to_str().unwrap();
-        let toplevel_stream = Stream::from_static_string(program, &mut self.machine_st.arena);
+        let toplevel_stream = match program {
+            Cow::Borrowed(s) => Stream::from_static_string(s, &mut self.machine_st.arena),
+            Cow::Owned(s) => Stream::from_owned_string(s, &mut self.machine_st.arena),
+        };
 
         self.load_file(path, toplevel_stream);