@@ -654,6 +654,7 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
654
654
new_ptr_info .sentinel = null ;
655
655
}
656
656
}
657
+ return @Type (std.builtin.TypeInfo { .Pointer = new_ptr_info });
657
658
},
658
659
else = > {},
659
660
},
@@ -668,15 +669,16 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
668
669
new_ptr_info .sentinel = null ;
669
670
}
670
671
}
672
+ return @Type (std.builtin.TypeInfo { .Pointer = new_ptr_info });
671
673
},
672
674
.C = > {
673
675
new_ptr_info .sentinel = end ;
674
676
// C pointers are always allowzero, but we don't want the return type to be.
675
677
assert (new_ptr_info .is_allowzero );
676
678
new_ptr_info .is_allowzero = false ;
679
+ return ? @Type (std.builtin.TypeInfo { .Pointer = new_ptr_info });
677
680
},
678
681
}
679
- return @Type (std.builtin.TypeInfo { .Pointer = new_ptr_info });
680
682
},
681
683
else = > {},
682
684
}
@@ -691,11 +693,11 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
691
693
/// Pointer properties such as mutability and alignment are preserved.
692
694
/// C pointers are assumed to be non-null.
693
695
pub fn sliceTo (ptr : anytype , comptime end : meta .Elem (@TypeOf (ptr ))) SliceTo (@TypeOf (ptr ), end ) {
694
- if (@typeInfo (@TypeOf (ptr )) == .Optional ) {
696
+ const Result = SliceTo (@TypeOf (ptr ), end );
697
+ if (@typeInfo (Result ) == .Optional ) {
695
698
const non_null = ptr orelse return null ;
696
699
return sliceTo (non_null , end );
697
700
}
698
- const Result = SliceTo (@TypeOf (ptr ), end );
699
701
const length = lenSliceTo (ptr , end );
700
702
if (@typeInfo (Result ).Pointer .sentinel ) | s | {
701
703
return ptr [0.. length :s ];
@@ -723,7 +725,9 @@ test "sliceTo" {
723
725
try testing .expectEqualSlices (u16 , array [0.. 4], sliceTo (optional_sentinel_ptr , 99 ).? );
724
726
725
727
const c_ptr = @as ([* c ]u16 , & array );
726
- try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (c_ptr , 3 ));
728
+ try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (c_ptr , 3 ).? );
729
+ const null_c_ptr = @as ([* c ]u16 , null );
730
+ try testing .expect (@as ([]u16 , null ), sliceTo (null_c_ptr , 3 ));
727
731
728
732
const slice : []u16 = & array ;
729
733
try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (slice , 3 ));
0 commit comments