Skip to content

Commit

Permalink
Dylib and webserver improvements, docs (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwparas authored Dec 24, 2023
1 parent bfc5a8d commit 9e3baec
Show file tree
Hide file tree
Showing 12 changed files with 606 additions and 182 deletions.
168 changes: 162 additions & 6 deletions crates/steel-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,29 @@ impl FromSteelVal for SteelString {
}
}

// impl<'a, T: AsRefSteelVal> PrimitiveAsRef<'a> for &'a T {
// #[inline(always)]
// fn primitive_as_ref(val: &'a SteelVal) -> crate::rvals::Result<Self> {
// AsRefSteelVal::as_ref(val, &mut <T as AsRefSteelVal>::Nursery::default())
// }
// }
pub(crate) enum Either<L, R> {
Left(L),
Right(R),
}

impl<'a, L: PrimitiveAsRef<'a>, R: PrimitiveAsRef<'a>> PrimitiveAsRef<'a> for Either<L, R> {
#[inline(always)]
fn primitive_as_ref(val: &'a SteelVal) -> crate::rvals::Result<Self> {
let left_type_name = std::any::type_name::<L>();
let right_type_name = std::any::type_name::<R>();

let error_thunk = crate::throw!(ConversionError => format!("Cannot convert steel value to the specified type: {} or {}", left_type_name, right_type_name));

Self::maybe_primitive_as_ref(val).ok_or_else(error_thunk)
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
L::maybe_primitive_as_ref(val)
.map(Either::Left)
.or_else(|| R::maybe_primitive_as_ref(val).map(Either::Right))
}
}

impl<'a> PrimitiveAsRef<'a> for &'a Gc<RefCell<SteelVal>> {
#[inline(always)]
Expand All @@ -409,6 +426,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a Gc<RefCell<SteelVal>> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel boxed value", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::Boxed(c) = val {
Some(c)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a HeapRef<SteelVal> {
Expand All @@ -420,6 +446,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a HeapRef<SteelVal> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel box", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::HeapAllocated(b) = val {
Some(b)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a char {
Expand All @@ -431,6 +466,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a char {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel character", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::CharV(c) = val {
Some(c)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for char {
Expand All @@ -442,6 +486,15 @@ impl<'a> PrimitiveAsRef<'a> for char {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel character", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::CharV(c) = val {
Some(*c)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for isize {
Expand All @@ -453,6 +506,15 @@ impl<'a> PrimitiveAsRef<'a> for isize {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel int", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::IntV(i) = val {
Some(*i)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a Gc<Vector<SteelVal>> {
Expand All @@ -464,6 +526,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a Gc<Vector<SteelVal>> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel vector", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::VectorV(p) = val {
Some(&p.0)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelVector {
Expand All @@ -475,6 +546,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a SteelVector {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel vector", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::VectorV(p) = val {
Some(p)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a Gc<im_rc::HashSet<SteelVal>> {
Expand All @@ -486,6 +566,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a Gc<im_rc::HashSet<SteelVal>> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel hashset", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::HashSetV(p) = val {
Some(&p.0)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelHashSet {
Expand All @@ -497,6 +586,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a SteelHashSet {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel hashset", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::HashSetV(p) = val {
Some(p)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a HeapRef<Vec<SteelVal>> {
Expand All @@ -508,6 +606,14 @@ impl<'a> PrimitiveAsRef<'a> for &'a HeapRef<Vec<SteelVal>> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel mutable vector", val))
}
}

fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::MutableVector(p) = val {
Some(p)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelPort {
Expand All @@ -519,6 +625,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a SteelPort {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel port", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::PortV(p) = val {
Some(p)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a List<SteelVal> {
Expand All @@ -530,13 +645,27 @@ impl<'a> PrimitiveAsRef<'a> for &'a List<SteelVal> {
crate::stop!(ConversionError => format!("Cannot convert steel value: {} to steel list", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::ListV(l) = val {
Some(l)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelVal {
#[inline(always)]
fn primitive_as_ref(val: &'a SteelVal) -> crate::rvals::Result<Self> {
Ok(val)
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
Some(val)
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelString {
Expand All @@ -548,6 +677,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a SteelString {
crate::stop!(TypeMismatch => format!("Cannot convert steel value: {} to steel string", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::StringV(s) = val {
Some(s)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a Gc<im_rc::HashMap<SteelVal, SteelVal>> {
Expand All @@ -559,6 +697,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a Gc<im_rc::HashMap<SteelVal, SteelVal>> {
crate::stop!(ConversionError => format!("Canto convert steel value: {} to hashmap", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::HashMapV(hm) = val {
Some(&hm.0)
} else {
None
}
}
}

impl<'a> PrimitiveAsRef<'a> for &'a SteelHashMap {
Expand All @@ -570,6 +717,15 @@ impl<'a> PrimitiveAsRef<'a> for &'a SteelHashMap {
crate::stop!(ConversionError => format!("Canto convert steel value: {} to hashmap", val))
}
}

#[inline(always)]
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self> {
if let SteelVal::HashMapV(hm) = val {
Some(hm)
} else {
None
}
}
}

impl IntoSteelVal for String {
Expand Down
3 changes: 2 additions & 1 deletion crates/steel-core/src/rvals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<T: Custom + 'static> CustomType for T {
}

fn as_serializable_steelval(&mut self) -> Option<SerializableSteelVal> {
self.into_serializable_steelval()
<T as Custom>::into_serializable_steelval(self)
}

fn drop_mut(&mut self, drop_handler: &mut IterativeDropHandler) {
Expand Down Expand Up @@ -312,6 +312,7 @@ pub trait FromSteelVal: Sized {

pub trait PrimitiveAsRef<'a>: Sized {
fn primitive_as_ref(val: &'a SteelVal) -> Result<Self>;
fn maybe_primitive_as_ref(val: &'a SteelVal) -> Option<Self>;
}

pub struct RestArgsIter<'a, T>(
Expand Down
2 changes: 1 addition & 1 deletion crates/steel-core/src/scheme/print.scm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@

(simple-display ")")]))]

[else (simple-displayln obj)]))
[else (simple-display obj)]))

(define (#%print obj collector)
(cond
Expand Down
Loading

0 comments on commit 9e3baec

Please sign in to comment.