Skip to content

Commit db3e0a2

Browse files
authored
Merge pull request sl-sh-dev#158 from sl-sh-dev/more-str-macros
More str macros
2 parents 2545bf0 + 121ad31 commit db3e0a2

File tree

10 files changed

+357
-396
lines changed

10 files changed

+357
-396
lines changed

bridge_adapters/src/lisp_adapters.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ where
118118
fn sl_from(value: T, vm: &mut SloshVm) -> VMResult<Self>;
119119
}
120120

121+
impl<T> SlFrom<Vec<T>> for Value
122+
where
123+
T: SlInto<Value>,
124+
{
125+
fn sl_from(value: Vec<T>, vm: &mut SloshVm) -> VMResult<Self> {
126+
let mut u = Vec::with_capacity(value.len());
127+
for v in value {
128+
u.push(v.sl_into(vm)?);
129+
}
130+
Ok(vm.alloc_vector(u))
131+
}
132+
}
133+
134+
impl<'a, T> SlFromRef<'a, slvm::Value> for Vec<T>
135+
where
136+
T: SlFromRef<'a, Value> + 'a + ?Sized,
137+
{
138+
fn sl_from_ref(value: slvm::Value, vm: &'a SloshVm) -> VMResult<Self> {
139+
let mut res = vec![];
140+
for val in value.iter(vm) {
141+
let t: T = val.sl_into_ref(vm)?;
142+
res.push(t);
143+
}
144+
Ok(res)
145+
}
146+
}
147+
121148
/// Inverse of [`SlFrom`]
122149
pub trait SlInto<T>: Sized
123150
where
@@ -164,6 +191,34 @@ where
164191
}
165192
}
166193

194+
pub trait SlFromRefMut<'a, T: BridgedType>
195+
where
196+
Self: Sized,
197+
{
198+
/// Converts to this type from the input type.
199+
fn sl_from_ref_mut(value: T, vm: &'a mut SloshVm) -> VMResult<Self>;
200+
}
201+
202+
pub trait SlIntoRefMut<'a, T>: Sized
203+
where
204+
T: 'a,
205+
Self: BridgedType,
206+
{
207+
/// Converts to this type from the input type.
208+
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<T>;
209+
}
210+
211+
impl<'a, T, U> SlIntoRefMut<'a, U> for T
212+
where
213+
T: BridgedType,
214+
U: SlFromRefMut<'a, T>,
215+
U: 'a,
216+
{
217+
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<U> {
218+
U::sl_from_ref_mut(self, vm)
219+
}
220+
}
221+
167222
/// Converts a [`BridgedType`] to some rust type
168223
pub trait SlAsRef<'a, T: ?Sized>
169224
where

bridge_adapters/src/lisp_adapters/numbers.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ impl SlFrom<()> for Value {
1010
}
1111
}
1212

13-
impl<'a> SlFromRef<'a, &'a Value> for () {
14-
fn sl_from_ref(value: &Value, _vm: &'a SloshVm) -> VMResult<()> {
13+
impl<'a> SlFromRef<'a, Value> for () {
14+
fn sl_from_ref(value: Value, _vm: &'a SloshVm) -> VMResult<()> {
1515
match value {
1616
Value::Nil => Ok(()),
1717
_ => Err(VMError::new_conversion(
@@ -35,11 +35,11 @@ impl SlFrom<u32> for Value {
3535
}
3636
}
3737

38-
impl<'a> SlFromRef<'a, &'a Value> for i32 {
39-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<i32> {
38+
impl<'a> SlFromRef<'a, Value> for i32 {
39+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<i32> {
4040
match value {
4141
Value::Int(num) => {
42-
let num = from_i56(num);
42+
let num = from_i56(&num);
4343
num.try_into().map_err(|_| {
4444
VMError::new_conversion(
4545
"Provided slosh value too small to fit desired type.".to_string(),
@@ -62,10 +62,10 @@ impl SlFrom<f64> for Value {
6262
}
6363
}
6464

65-
impl<'a> SlFromRef<'a, &'a Value> for f64 {
66-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
65+
impl<'a> SlFromRef<'a, Value> for f64 {
66+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
6767
match value {
68-
Value::Float(f56) => Ok(f64::from(*f56)),
68+
Value::Float(f56) => Ok(f64::from(f56)),
6969
_ => Err(VMError::new_conversion(
7070
ErrorStrings::fix_me_mismatched_type(
7171
<&'static str>::from(ValueType::Float),
@@ -76,10 +76,16 @@ impl<'a> SlFromRef<'a, &'a Value> for f64 {
7676
}
7777
}
7878

79-
impl<'a> SlFromRef<'a, &'a Value> for usize {
80-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
79+
impl SlFrom<usize> for Value {
80+
fn sl_from(value: usize, _vm: &mut SloshVm) -> VMResult<Self> {
81+
Ok(to_i56(value as i64))
82+
}
83+
}
84+
85+
impl<'a> SlFromRef<'a, Value> for usize {
86+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
8187
match value {
82-
Value::Int(i) => usize::try_from(from_i56(i)).map_err(|_| {
88+
Value::Int(i) => usize::try_from(from_i56(&i)).map_err(|_| {
8389
VMError::new_conversion(ErrorStrings::fix_me_mismatched_type(
8490
<&'static str>::from(ValueType::Int),
8591
value.display_type(vm),
@@ -95,10 +101,10 @@ impl<'a> SlFromRef<'a, &'a Value> for usize {
95101
}
96102
}
97103

98-
impl<'a> SlFromRef<'a, &'a Value> for i64 {
99-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
104+
impl<'a> SlFromRef<'a, Value> for i64 {
105+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
100106
match value {
101-
Value::Int(i) => Ok(from_i56(i)),
107+
Value::Int(i) => Ok(from_i56(&i)),
102108
_ => Err(VMError::new_conversion(
103109
ErrorStrings::fix_me_mismatched_type(
104110
<&'static str>::from(ValueType::Int),
@@ -132,6 +138,6 @@ mod tests {
132138
let mut vm = new_slosh_vm();
133139
let vm = &mut vm;
134140
let val = to_i56(7_i32 as i64);
135-
let _val: i32 = i32::sl_from_ref(&val, vm).expect("Value can be converted to i32");
141+
let _val: i32 = i32::sl_from_ref(val, vm).expect("Value can be converted to i32");
136142
}
137143
}

bridge_adapters/src/lisp_adapters/primitives.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::lisp_adapters::{SlFrom, SlFromRef};
22
use compile_state::state::SloshVm;
33
use slvm::{VMResult, Value};
44

5-
impl<'a> SlFromRef<'a, &'a Value> for bool {
6-
fn sl_from_ref(value: &Value, _vm: &SloshVm) -> VMResult<Self> {
5+
impl<'a> SlFromRef<'a, Value> for bool {
6+
fn sl_from_ref(value: Value, _vm: &SloshVm) -> VMResult<Self> {
77
match value {
88
Value::True => Ok(true),
99
Value::False => Ok(false),
@@ -39,11 +39,11 @@ where
3939
}
4040
}
4141

42-
impl<'a, T> SlFromRef<'a, &'a Value> for Option<T>
42+
impl<'a, T> SlFromRef<'a, Value> for Option<T>
4343
where
44-
T: SlFromRef<'a, &'a Value>,
44+
T: SlFromRef<'a, Value>,
4545
{
46-
fn sl_from_ref(value: &'a Value, vm: &'a SloshVm) -> VMResult<Self> {
46+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
4747
if value.is_nil() {
4848
Ok(None)
4949
} else {
@@ -77,19 +77,19 @@ mod tests {
7777
let mut vm = new_slosh_vm();
7878
let vm = &mut vm;
7979
let n = Value::Nil;
80-
let n: bool = (&n)
80+
let n: bool = n
8181
.sl_into_ref(vm)
8282
.expect("Value::Nil can be converted to bool");
8383
assert_eq!(n, false);
8484

8585
let f = Value::False;
86-
let f: bool = (&f)
86+
let f: bool = f
8787
.sl_into_ref(vm)
8888
.expect("Value::False can be converted to bool");
8989
assert_eq!(f, false);
9090

9191
let t = Value::True;
92-
let t: bool = (&t)
92+
let t: bool = t
9393
.sl_into_ref(vm)
9494
.expect("Value::True can be converted to bool");
9595
assert_eq!(t, true);

bridge_adapters/src/lisp_adapters/text.rs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lisp_adapters::{SlAsMut, SlAsRef, SlFrom, SlFromRef};
1+
use crate::lisp_adapters::{SlAsMut, SlAsRef, SlFrom, SlFromRef, SlFromRefMut};
22
use bridge_types::{ErrorStrings, LooseString, SloshChar};
33
use compile_state::state::SloshVm;
44
use slvm::value::ValueType;
@@ -17,22 +17,22 @@ impl<'a> SlFrom<SloshChar<'a>> for Value {
1717
}
1818
}
1919

20-
impl<'a> SlFromRef<'a, &Value> for LooseString<'a> {
21-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
20+
impl<'a> SlFromRef<'a, Value> for LooseString<'a> {
21+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
2222
match value {
23-
Value::String(h) => Ok(LooseString::Borrowed(vm.get_string(*h))),
23+
Value::String(h) => Ok(LooseString::Borrowed(vm.get_string(h))),
2424
Value::CodePoint(char) => Ok(LooseString::Owned(char.to_string())),
2525
Value::CharCluster(l, c) => Ok(LooseString::Owned(format!(
2626
"{}",
27-
String::from_utf8_lossy(&c[0..*l as usize])
27+
String::from_utf8_lossy(&c[0..l as usize])
2828
))),
2929
Value::CharClusterLong(h) => {
30-
let ch = vm.get_string(*h);
30+
let ch = vm.get_string(h);
3131
Ok(LooseString::Borrowed(ch))
3232
}
33-
Value::Symbol(i) => Ok(LooseString::Borrowed(vm.get_interned(*i))),
34-
Value::Keyword(i) => Ok(LooseString::Borrowed(vm.get_interned(*i))),
35-
Value::StringConst(i) => Ok(LooseString::Borrowed(vm.get_interned(*i))),
33+
Value::Symbol(i) => Ok(LooseString::Borrowed(vm.get_interned(i))),
34+
Value::Keyword(i) => Ok(LooseString::Borrowed(vm.get_interned(i))),
35+
Value::StringConst(i) => Ok(LooseString::Borrowed(vm.get_interned(i))),
3636
_ => Err(VMError::new_conversion(
3737
ErrorStrings::fix_me_mismatched_type(
3838
String::from(ValueTypes::from([
@@ -51,10 +51,10 @@ impl<'a> SlFromRef<'a, &Value> for LooseString<'a> {
5151
}
5252
}
5353

54-
impl<'a> SlFromRef<'a, &'a Value> for char {
55-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
54+
impl<'a> SlFromRef<'a, Value> for char {
55+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
5656
match value {
57-
Value::CodePoint(char) => Ok(*char),
57+
Value::CodePoint(char) => Ok(char),
5858
_ => Err(VMError::new_conversion(
5959
ErrorStrings::fix_me_mismatched_type_with_context(
6060
String::from(ValueTypes::from([ValueType::CodePoint])),
@@ -72,12 +72,11 @@ impl SlFrom<char> for Value {
7272
}
7373
}
7474

75-
impl<'a> SlFromRef<'a, &Value> for &'a str {
76-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
77-
value.sl_as_ref(vm)
75+
impl<'a> SlFromRef<'a, Value> for &'a str {
76+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
77+
(&value).sl_as_ref(vm)
7878
}
7979
}
80-
8180
impl<'a> SlAsRef<'a, str> for &Value {
8281
fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a str> {
8382
match self {
@@ -96,15 +95,15 @@ impl<'a> SlAsRef<'a, str> for &Value {
9695
}
9796
}
9897

99-
impl<'a> SlFromRef<'a, &Value> for SloshChar<'a> {
100-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
98+
impl<'a> SlFromRef<'a, Value> for SloshChar<'a> {
99+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
101100
match value {
102-
Value::CodePoint(ch) => Ok(SloshChar::Char(*ch)),
101+
Value::CodePoint(ch) => Ok(SloshChar::Char(ch)),
103102
Value::CharCluster(l, c) => Ok(SloshChar::String(Cow::Owned(format!(
104103
"{}",
105-
String::from_utf8_lossy(&c[0..*l as usize])
104+
String::from_utf8_lossy(&c[0..l as usize])
106105
)))),
107-
Value::CharClusterLong(h) => Ok(SloshChar::String(Cow::Borrowed(vm.get_string(*h)))),
106+
Value::CharClusterLong(h) => Ok(SloshChar::String(Cow::Borrowed(vm.get_string(h)))),
108107
_ => Err(VMError::new_conversion(
109108
ErrorStrings::fix_me_mismatched_type(
110109
String::from(ValueTypes::from([
@@ -119,6 +118,12 @@ impl<'a> SlFromRef<'a, &Value> for SloshChar<'a> {
119118
}
120119
}
121120

121+
impl<'a> SlFromRefMut<'a, Value> for &'a mut String {
122+
fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> VMResult<Self> {
123+
(&value).sl_as_mut(vm)
124+
}
125+
}
126+
122127
impl<'a> SlAsMut<'a, String> for &Value {
123128
fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut String> {
124129
match self {
@@ -157,10 +162,10 @@ where
157162
}
158163
}
159164

160-
impl<'a> SlFromRef<'a, &'a Value> for String {
161-
fn sl_from_ref(value: &Value, vm: &'a SloshVm) -> VMResult<Self> {
165+
impl<'a> SlFromRef<'a, Value> for String {
166+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
162167
match value {
163-
Value::String(h) => Ok(vm.get_string(*h).to_string()),
168+
Value::String(h) => Ok(vm.get_string(h).to_string()),
164169
_ => Err(VMError::new_conversion(
165170
ErrorStrings::fix_me_mismatched_type(
166171
<&'static str>::from(ValueType::String),
@@ -252,7 +257,7 @@ mod tests {
252257
fn try_conversion_error() {
253258
let mut vm = new_slosh_vm();
254259
let value = create_string(&mut vm);
255-
let c: VMResult<char> = (&value).sl_into_ref(&mut vm);
260+
let c: VMResult<char> = value.sl_into_ref(&vm);
256261
assert!(c.is_err());
257262
let err = VMError::new_conversion(ErrorStrings::fix_me_mismatched_type_with_context(
258263
String::from(ValueTypes::from([ValueType::CodePoint])),
@@ -362,7 +367,7 @@ mod tests {
362367
}
363368
_ => {
364369
return {
365-
let arg: String = arg_0.sl_into_ref(vm)?;
370+
let arg: String = (*arg_0).sl_into_ref(vm)?;
366371
arg.trim().to_string().sl_into(vm)
367372
}
368373
}
@@ -387,12 +392,12 @@ mod tests {
387392
.expect("&mut String can be converted to Value");
388393
assert!(matches!(val, Value::String(_)));
389394

390-
let _s: String = (&val)
395+
let _s: String = val
391396
.sl_into_ref(vm)
392397
.expect("&Value::String can be converted to String");
393398
let kwd_val = create_keyword(vm);
394399

395-
let e: VMResult<String> = (&kwd_val).sl_into_ref(vm);
400+
let e: VMResult<String> = kwd_val.sl_into_ref(vm);
396401
e.expect_err("Can not convert keyword to String");
397402

398403
let _s: &str = (&val)
@@ -446,7 +451,7 @@ mod tests {
446451
let vm = &mut vm;
447452

448453
let val = create_code_point();
449-
let _c: char = (&val)
454+
let _c: char = val
450455
.sl_into_ref(vm)
451456
.expect("&Value::CodePoint can be converted to char");
452457
}
@@ -468,12 +473,12 @@ mod tests {
468473
let vm = &mut vm;
469474

470475
let val = create_char_cluster(vm);
471-
let _c: SloshChar = (&val)
476+
let _c: SloshChar = val
472477
.sl_into_ref(vm)
473478
.expect("&Value::CharCluster can be converted to SloshChar");
474479

475480
let val = create_char_cluster_long(vm);
476-
let _c: SloshChar = (&val)
481+
let _c: SloshChar = val
477482
.sl_into_ref(vm)
478483
.expect("&Value::CharClusterLong can be converted to SloshChar");
479484
}
@@ -514,7 +519,7 @@ mod tests {
514519
let loose_strings_as_vals = get_values_that_can_be_cast_to_loose_strings(vm);
515520

516521
for val in loose_strings_as_vals {
517-
let _loose_string: LooseString = (&val)
522+
let _loose_string: LooseString = val
518523
.sl_into_ref(vm)
519524
.expect("This value should be convertable to a LooseString");
520525
}

0 commit comments

Comments
 (0)