Browse Source

add as_varient functions to OwnedAttributeValue

Evan Almloff 2 years ago
parent
commit
21edea62be
2 changed files with 56 additions and 0 deletions
  1. 37 0
      packages/native-core/src/node.rs
  2. 19 0
      packages/native-core/src/worker_pool.rs

+ 37 - 0
packages/native-core/src/node.rs

@@ -83,3 +83,40 @@ pub enum OwnedAttributeValue {
     Bool(bool),
     None,
 }
+
+impl OwnedAttributeValue {
+    pub fn as_text(&self) -> Option<&str> {
+        match self {
+            OwnedAttributeValue::Text(text) => Some(text),
+            _ => None,
+        }
+    }
+
+    pub fn as_float(&self) -> Option<f32> {
+        match self {
+            OwnedAttributeValue::Float(float) => Some(*float),
+            _ => None,
+        }
+    }
+
+    pub fn as_int(&self) -> Option<i32> {
+        match self {
+            OwnedAttributeValue::Int(int) => Some(*int),
+            _ => None,
+        }
+    }
+
+    pub fn as_bool(&self) -> Option<bool> {
+        match self {
+            OwnedAttributeValue::Bool(bool) => Some(*bool),
+            _ => None,
+        }
+    }
+
+    pub fn as_none(&self) -> Option<()> {
+        match self {
+            OwnedAttributeValue::None => Some(()),
+            _ => None,
+        }
+    }
+}

+ 19 - 0
packages/native-core/src/worker_pool.rs

@@ -1 +1,20 @@
+use crossbeam_deque::{Injector, Stealer, Worker};
+use std::iter;
 
+fn find_task<T>(local: &Worker<T>, global: &Injector<T>, stealers: &[Stealer<T>]) -> Option<T> {
+    // Pop a task from the local queue, if not empty.
+    local.pop().or_else(|| {
+        // Otherwise, we need to look for a task elsewhere.
+        iter::repeat_with(|| {
+            // Try stealing a batch of tasks from the global queue.
+            global
+                .steal_batch_and_pop(local)
+                // Or try stealing a task from one of the other threads.
+                .or_else(|| stealers.iter().map(|s| s.steal()).collect())
+        })
+        // Loop while no task was stolen and any steal operation needs to be retried.
+        .find(|s| !s.is_retry())
+        // Extract the stolen task, if there is one.
+        .and_then(|s| s.success())
+    })
+}