@@ -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
}
@@ -689,13 +691,12 @@ fn SliceTo(comptime T: type, comptime end: meta.Elem(T)) type {
689
691
/// If the pointer type is sentinel terminated and `end` matches that terminator, the
690
692
/// resulting slice is also sentinel terminated.
691
693
/// Pointer properties such as mutability and alignment are preserved.
692
- /// C pointers are assumed to be non-null.
693
694
pub fn sliceTo (ptr : anytype , comptime end : meta .Elem (@TypeOf (ptr ))) SliceTo (@TypeOf (ptr ), end ) {
694
- if (@typeInfo (@TypeOf (ptr )) == .Optional ) {
695
+ const Result = SliceTo (@TypeOf (ptr ), end );
696
+ if (@typeInfo (Result ) == .Optional ) {
695
697
const non_null = ptr orelse return null ;
696
698
return sliceTo (non_null , end );
697
699
}
698
- const Result = SliceTo (@TypeOf (ptr ), end );
699
700
const length = lenSliceTo (ptr , end );
700
701
if (@typeInfo (Result ).Pointer .sentinel ) | s | {
701
702
return ptr [0.. length :s ];
@@ -723,7 +724,9 @@ test "sliceTo" {
723
724
try testing .expectEqualSlices (u16 , array [0.. 4], sliceTo (optional_sentinel_ptr , 99 ).? );
724
725
725
726
const c_ptr = @as ([* c ]u16 , & array );
726
- try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (c_ptr , 3 ));
727
+ try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (c_ptr , 3 ).? );
728
+ const null_c_ptr = @as (? [* c ]u16 , null );
729
+ try testing .expect (@as ([]u16 , null ), sliceTo (null_c_ptr , 3 ));
727
730
728
731
const slice : []u16 = & array ;
729
732
try testing .expectEqualSlices (u16 , array [0.. 2], sliceTo (slice , 3 ));
0 commit comments