on master help sign in
Add a comment about a slice invariant
commit3fbdd64b3c194690bf86db535fbcdc1cf666359c authorHarendra Kumar <harendra@composewell.com> dateFri 17 Dec 2021, 14:18 (5 years ago) parent9e3347e
1 file changed+4
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4
@@ -1138,6 +1138,8 @@ getSliceUnsafe index len (Array contents start e _) =
11381138 end = fp1 `plusPtr` (len * SIZE_OF(a))
11391139 in assert
11401140 (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.
11411143 (Array contents fp1 end end)
11421144
11431145-- | /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 _) =
11541156 let fp1 = PTR_INDEX(start,index,a)
11551157 end = fp1 `plusPtr` (len * SIZE_OF(a))
11561158 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.
11571161 then Array contents fp1 end end
11581162 else error
11591163 $ "getSlice: invalid slice, index "