Skip to content

Commit 2625e8e

Browse files
committed
Refactor shard creation to use AutoShardRef
- Replace instances of ShardRef::create with AutoShardRef::create in eval.rs and types.rs for consistency and improved functionality. - Remove redundant AutoShardRef wrapping after creation. - Update related comments to reflect changes in shard creation logic.
1 parent e372e7b commit 2625e8e

File tree

2 files changed

+52
-64
lines changed

2 files changed

+52
-64
lines changed

shards/lang/src/eval.rs

+26-42
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ fn extract_make_ints_shard<const WIDTH: usize>(
285285
}
286286

287287
let shard = match WIDTH {
288-
2 => ShardRef::create("MakeInt2", Some(line_info.into())),
289-
3 => ShardRef::create("MakeInt3", Some(line_info.into())),
290-
4 => ShardRef::create("MakeInt4", Some(line_info.into())),
291-
8 => ShardRef::create("MakeInt8", Some(line_info.into())),
292-
16 => ShardRef::create("MakeInt16", Some(line_info.into())),
288+
2 => AutoShardRef::create("MakeInt2", Some(line_info.into())),
289+
3 => AutoShardRef::create("MakeInt3", Some(line_info.into())),
290+
4 => AutoShardRef::create("MakeInt4", Some(line_info.into())),
291+
8 => AutoShardRef::create("MakeInt8", Some(line_info.into())),
292+
16 => AutoShardRef::create("MakeInt16", Some(line_info.into())),
293293
_ => {
294294
return Err(
295295
(
@@ -301,7 +301,6 @@ fn extract_make_ints_shard<const WIDTH: usize>(
301301
}
302302
}
303303
.unwrap(); // qed, those shards must exist!
304-
let shard = AutoShardRef(shard);
305304

306305
for i in 0..len {
307306
let var = match &params[i].value {
@@ -504,9 +503,9 @@ fn extract_make_floats_shard<const WIDTH: usize>(
504503
}
505504

506505
let shard = match WIDTH {
507-
2 => ShardRef::create("MakeFloat2", Some(line_info.into())),
508-
3 => ShardRef::create("MakeFloat3", Some(line_info.into())),
509-
4 => ShardRef::create("MakeFloat4", Some(line_info.into())),
506+
2 => AutoShardRef::create("MakeFloat2", Some(line_info.into())),
507+
3 => AutoShardRef::create("MakeFloat3", Some(line_info.into())),
508+
4 => AutoShardRef::create("MakeFloat4", Some(line_info.into())),
510509
_ => {
511510
return Err(
512511
(
@@ -518,7 +517,6 @@ fn extract_make_floats_shard<const WIDTH: usize>(
518517
}
519518
}
520519
.unwrap(); // qed, those shards must exist!
521-
let shard = AutoShardRef(shard);
522520

523521
for i in 0..len {
524522
let var = match &params[i].value {
@@ -642,7 +640,7 @@ fn extract_make_colors_shard(
642640
)
643641
}
644642

645-
let shard = AutoShardRef(ShardRef::create("MakeColor", Some(line_info.into())).unwrap()); // qed, this shard must exist!
643+
let shard = AutoShardRef::create("MakeColor", Some(line_info.into())).unwrap(); // qed, this shard must exist!
646644

647645
for i in 0..len {
648646
let var = match &params[i].value {
@@ -1747,9 +1745,9 @@ impl<'e> VariableResolver<'e> {
17471745
BlockContent::EvalExpr(seq) => Value::EvalExpr(seq.clone()),
17481746
BlockContent::Expr(seq) => Value::Expr(seq.clone()),
17491747
BlockContent::Const(value) => value.clone(),
1750-
_ => Value::None(())
1748+
_ => Value::None(()),
17511749
}
1752-
},
1750+
}
17531751
_ => Value::None(()),
17541752
};
17551753
self.resolve_var(&value, line_info, shard)
@@ -2252,16 +2250,14 @@ fn create_shard_inner(
22522250
shard
22532251
};
22542252

2255-
let s = ShardRef::create(shard.name.name.as_str(), Some(line_info.into())).ok_or(
2253+
let s = AutoShardRef::create(shard.name.name.as_str(), Some(line_info.into())).ok_or(
22562254
(
22572255
format!("Shard {} does not exist", shard.name.name.as_str()),
22582256
line_info,
22592257
)
22602258
.into(),
22612259
)?;
22622260

2263-
let s = AutoShardRef(s);
2264-
22652261
let mut idx = 0i32;
22662262
let mut as_idx = true;
22672263
if let Some(ref params) = shard.params {
@@ -2413,8 +2409,7 @@ fn add_const_shard2(
24132409
line_info: LineInfo,
24142410
e: &mut EvalEnv,
24152411
) -> Result<(), ShardsError> {
2416-
let shard = ShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
2417-
let shard = AutoShardRef(shard);
2412+
let shard = AutoShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
24182413
shard
24192414
.0
24202415
.set_parameter(0, value)
@@ -2425,8 +2420,7 @@ fn add_const_shard2(
24252420
}
24262421

24272422
fn add_const_shard3(value: Var, line_info: LineInfo, e: &mut EvalEnv) -> Result<(), ShardsError> {
2428-
let shard = ShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
2429-
let shard = AutoShardRef(shard);
2423+
let shard = AutoShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
24302424
shard
24312425
.0
24322426
.set_parameter(0, value)
@@ -2442,18 +2436,16 @@ fn into_get_or_const(
24422436
) -> Result<AutoShardRef, ShardsError> {
24432437
match resolve_var(&value, line_info, None, e)? {
24442438
ResolvedVar::Constant(var) => {
2445-
let shard = ShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
2446-
let shard = AutoShardRef(shard);
2439+
let shard = AutoShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
24472440
shard
24482441
.0
24492442
.set_parameter(0, *var.as_ref())
24502443
.map_err(|e| (format!("{}", e), line_info).into())?;
24512444
Ok(shard)
24522445
}
24532446
ResolvedVar::Variable(var) => {
2454-
let shard = ShardRef::create("Get", Some(line_info.into())).unwrap(); // qed, Get must exist
2455-
let shard = AutoShardRef(shard);
2456-
// todo - avoid clone
2447+
let shard = AutoShardRef::create("Get", Some(line_info.into())).unwrap(); // qed, Get must exist
2448+
// todo - avoid clone
24572449
shard
24582450
.0
24592451
.set_parameter(0, *var.as_ref())
@@ -2491,8 +2483,7 @@ fn add_const_shard(value: &Value, line_info: LineInfo, e: &mut EvalEnv) -> Resul
24912483
| Value::TakeSeq(_, _)
24922484
| Value::Func(_)
24932485
| Value::Table(_) => {
2494-
let shard = ShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
2495-
let shard = AutoShardRef(shard);
2486+
let shard = AutoShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
24962487
let value = as_var(&replacement.clone(), line_info, Some(shard.0), e)?;
24972488
shard
24982489
.0
@@ -2521,8 +2512,7 @@ fn add_const_shard(value: &Value, line_info: LineInfo, e: &mut EvalEnv) -> Resul
25212512
.map(|shard| shard_with_id_iden(shard, e, name))
25222513
}
25232514
_ => {
2524-
let shard = ShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
2525-
let shard = AutoShardRef(shard);
2515+
let shard = AutoShardRef::create("Const", Some(line_info.into())).unwrap(); // qed, Const must exist
25262516
let value = as_var(value, line_info, Some(shard.0), e)?;
25272517
shard
25282518
.0
@@ -2542,8 +2532,7 @@ fn make_sub_shard(
25422532
shards: Vec<AutoShardRef>,
25432533
line_info: LineInfo,
25442534
) -> Result<AutoShardRef, ShardsError> {
2545-
let shard = ShardRef::create("SubFlow", Some(line_info.into())).unwrap(); // qed, Sub must exist
2546-
let shard = AutoShardRef(shard);
2535+
let shard = AutoShardRef::create("SubFlow", Some(line_info.into())).unwrap(); // qed, Sub must exist
25472536
let mut seq = AutoSeqVar::new();
25482537
for shard in shards {
25492538
let s = shard.0 .0;
@@ -2564,8 +2553,7 @@ fn add_take_shard(
25642553
line_info: LineInfo,
25652554
e: &mut EvalEnv,
25662555
) -> Result<(), ShardsError> {
2567-
let shard = ShardRef::create("Take", Some(line_info.into())).unwrap(); // qed, Take must exist
2568-
let shard = AutoShardRef(shard);
2556+
let shard = AutoShardRef::create("Take", Some(line_info.into())).unwrap(); // qed, Take must exist
25692557
shard
25702558
.0
25712559
.set_parameter(0, *target)
@@ -2576,8 +2564,7 @@ fn add_take_shard(
25762564
}
25772565

25782566
fn add_get_shard(name: &Identifier, line: LineInfo, e: &mut EvalEnv) -> Result<(), ShardsError> {
2579-
let shard = ShardRef::create("Get", Some(line.into())).unwrap(); // qed, Get must exist
2580-
let shard = AutoShardRef(shard);
2567+
let shard = AutoShardRef::create("Get", Some(line.into())).unwrap(); // qed, Get must exist
25812568
let (full_name, is_replacement) = get_full_name(name, e, line, name.namespaces.is_empty())?;
25822569
let suffix = if !is_replacement {
25832570
// suffix is only relevant if we are not a replacement
@@ -2605,8 +2592,7 @@ fn add_get_shard(name: &Identifier, line: LineInfo, e: &mut EvalEnv) -> Result<(
26052592
}
26062593

26072594
fn add_get_shard_no_suffix(name: &str, line: LineInfo, e: &mut EvalEnv) -> Result<(), ShardsError> {
2608-
let shard = ShardRef::create("Get", Some(line.into())).unwrap(); // qed, Get must exist
2609-
let shard = AutoShardRef(shard);
2595+
let shard = AutoShardRef::create("Get", Some(line.into())).unwrap(); // qed, Get must exist
26102596
let name = Var::ephemeral_string(name);
26112597
shard
26122598
.0
@@ -3390,7 +3376,7 @@ fn eval_pipeline(
33903376
}
33913377
("run", true) => {
33923378
if let Some(ref params) = func.params {
3393-
let mut otherFn = func.clone();
3379+
let mut otherFn = func.clone();
33943380
otherFn.name.name = RcStrWrapper::new("Run");
33953381
add_shard(&otherFn, block.line_info.unwrap_or_default(), e)?;
33963382
Ok(())
@@ -3688,8 +3674,7 @@ fn add_assignment_shard(
36883674
line_info: LineInfo,
36893675
e: &mut EvalEnv,
36903676
) -> Result<(), ShardsError> {
3691-
let shard = ShardRef::create(shard_name, Some(line_info.into())).unwrap(); // qed shard_name shard should exist
3692-
let shard = AutoShardRef(shard);
3677+
let shard = AutoShardRef::create(shard_name, Some(line_info.into())).unwrap(); // qed shard_name shard should exist
36933678
let (full_name, is_replacement) = get_full_name(name, e, line_info, name.namespaces.is_empty())?;
36943679
let suffix = if !is_replacement {
36953680
// suffix is only relevant if we are not a replacement
@@ -3748,8 +3733,7 @@ fn add_assignment_shard_no_suffix(
37483733
line_info: LineInfo,
37493734
e: &mut EvalEnv,
37503735
) -> Result<(), ShardsError> {
3751-
let shard = ShardRef::create(shard_name, Some(line_info.into())).unwrap(); // qed shard_name shard should exist
3752-
let shard = AutoShardRef(shard);
3736+
let shard = AutoShardRef::create(shard_name, Some(line_info.into())).unwrap(); // qed shard_name shard should exist
37533737
let name = Var::ephemeral_string(name);
37543738
shard
37553739
.0

shards/rust/src/types.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,27 @@ unsafe extern "C" fn error_cb(
524524
}
525525
}
526526

527+
impl AutoShardRef {
528+
pub fn create(name: &str, debug_info: Option<(u32, u32)>) -> Option<Self> {
529+
unsafe {
530+
let ptr = (*Core).createShard.unwrap_unchecked()(SHStringWithLen {
531+
string: name.as_ptr() as *const c_char,
532+
len: name.len() as u64,
533+
});
534+
if ptr.is_null() {
535+
None
536+
} else {
537+
if let Some(debug_info) = debug_info {
538+
(*ptr).line = debug_info.0;
539+
(*ptr).column = debug_info.1;
540+
}
541+
(*ptr).setup.unwrap_unchecked()(ptr);
542+
Some(AutoShardRef(ShardRef(ptr)))
543+
}
544+
}
545+
}
546+
}
547+
527548
impl ShardRef {
528549
pub fn output_types(&self) -> &[Type] {
529550
unsafe {
@@ -545,25 +566,6 @@ impl ShardRef {
545566
}
546567
}
547568

548-
pub fn create(name: &str, debug_info: Option<(u32, u32)>) -> Option<Self> {
549-
unsafe {
550-
let ptr = (*Core).createShard.unwrap_unchecked()(SHStringWithLen {
551-
string: name.as_ptr() as *const c_char,
552-
len: name.len() as u64,
553-
});
554-
if ptr.is_null() {
555-
None
556-
} else {
557-
if let Some(debug_info) = debug_info {
558-
(*ptr).line = debug_info.0;
559-
(*ptr).column = debug_info.1;
560-
}
561-
(*ptr).setup.unwrap_unchecked()(ptr);
562-
Some(ShardRef(ptr))
563-
}
564-
}
565-
}
566-
567569
pub fn name(&self) -> &str {
568570
unsafe {
569571
let c_name = (*self.0).name.unwrap_unchecked()(self.0);
@@ -3580,7 +3582,7 @@ impl Var {
35803582

35813583
impl TryFrom<&Var> for SHString {
35823584
type Error = &'static str;
3583-
3585+
35843586
#[inline(always)]
35853587
fn try_from(var: &Var) -> Result<Self, Self::Error> {
35863588
if var.valueType != SHType_String
@@ -6374,8 +6376,10 @@ lazy_static! {
63746376
pub static ref STRINGS_TYPES: Vec<Type> = vec![common_type::strings];
63756377
pub static ref SEQ_OF_STRINGS: Type = Type::seq(&STRINGS_TYPES);
63766378
pub static ref SEQ_OF_STRINGS_TYPES: Vec<Type> = vec![*SEQ_OF_STRINGS];
6377-
pub static ref SEQ_OF_STRINGS_OR_SEQ_OF_BYTES_TYPES: Vec<Type> = vec![*SEQ_OF_STRINGS, *SEQ_OF_BYTES];
6378-
pub static ref SEQ_OF_STRING_OR_BYTE_TYPES: Vec<Type> = vec![common_type::string, common_type::bytes];
6379+
pub static ref SEQ_OF_STRINGS_OR_SEQ_OF_BYTES_TYPES: Vec<Type> =
6380+
vec![*SEQ_OF_STRINGS, *SEQ_OF_BYTES];
6381+
pub static ref SEQ_OF_STRING_OR_BYTE_TYPES: Vec<Type> =
6382+
vec![common_type::string, common_type::bytes];
63796383
pub static ref COLOR_TYPES: Vec<Type> = vec![common_type::color];
63806384
pub static ref INT_TYPES: Vec<Type> = vec![common_type::int];
63816385
pub static ref INT2_TYPES: Vec<Type> = vec![common_type::int2];

0 commit comments

Comments
 (0)