on master help sign in
Add a comment about a slice invariant
3fbdd64Harendra Kumar
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4
@@ -1138,6 +1138,8 @@ getSliceUnsafe index len (Array contents start e _) =
1138 end = fp1 `plusPtr` (len * SIZE_OF(a))
1138 end = fp1 `plusPtr` (len * SIZE_OF(a))
1139 in assert
1139 in assert
1140 (index >= 0 && len >= 0 && end <= e)
1140 (index >= 0 && len >= 0 && end <= e)
1141+ -- Note: In a slice we always use bound = end so that the slice
1142+ -- user cannot overwrite elements beyond the end of the slice.
1141 (Array contents fp1 end end)
1143 (Array contents fp1 end end)
1142
1144
1143-- | /O(1)/ Slice an array in constant time. Throws an error if the slice
1145-- | /O(1)/ Slice an array in constant time. Throws an error if the slice
@@ -1154,6 +1156,8 @@ getSlice index len (Array contents start e _) =
1154 let fp1 = PTR_INDEX(start,index,a)
1156 let fp1 = PTR_INDEX(start,index,a)
1155 end = fp1 `plusPtr` (len * SIZE_OF(a))
1157 end = fp1 `plusPtr` (len * SIZE_OF(a))
1156 in if index >= 0 && len >= 0 && end <= e
1158 in if index >= 0 && len >= 0 && end <= e
1159+ -- Note: In a slice we always use bound = end so that the slice user
1160+ -- cannot overwrite elements beyond the end of the slice.
1157 then Array contents fp1 end end
1161 then Array contents fp1 end end
1158 else error
1162 else error
1159 $ "getSlice: invalid slice, index "
1163 $ "getSlice: invalid slice, index "