Skip to content

Commit 323e5a2

Browse files
committed
improve safety
1 parent 93e2aab commit 323e5a2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

source/mir/ndslice/allocation.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ auto slice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterato
8585
alias fun = .slice;
8686
else
8787
alias fun = .uninitSlice;
88-
auto ret = fun!T(slice.shape);
88+
auto ret = (shape)@trusted{ return fun!T(shape);}(slice.shape);
8989
ret[] = slice;
9090
auto retq = ()@trusted{ return (cast(slice.DeepElemType*)ret._iterator).sliced(ret.shape); }();
9191
return retq;
@@ -122,7 +122,7 @@ auto slice(size_t dim, Slices...)(Concatenation!(dim, Slices) concatenation)
122122
alias fun = .slice;
123123
else
124124
alias fun = .uninitSlice;
125-
auto ret = fun!T(concatenation.shape);
125+
auto ret = (shape)@trusted{ return fun!T(shape);}(concatenation.shape);
126126
ret[] = concatenation;
127127
return ret;
128128
}

source/mir/ndslice/sorting.d

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Macros:
2424
module mir.ndslice.sorting;
2525

2626
/// Check if ndslice is sorted, or strictly monotonic.
27-
version(mir_test) unittest
27+
version(mir_test) @safe pure unittest
2828
{
2929
import mir.ndslice.algorithm: all;
3030
import mir.ndslice.slice: sliced;
@@ -48,7 +48,7 @@ version(mir_test) unittest
4848
}
4949

5050
/// Create index
51-
unittest
51+
version(mir_test) unittest
5252
{
5353
import mir.ndslice.algorithm: all;
5454
import mir.ndslice.allocation: slice;
@@ -65,7 +65,7 @@ unittest
6565
}
6666

6767
/// Schwartzian transform
68-
unittest
68+
version(mir_test) unittest
6969
{
7070
import mir.ndslice.algorithm: all;
7171
import mir.ndslice.allocation: slice;
@@ -128,7 +128,7 @@ template isStrictlyMonotonic(alias less = "a < b")
128128
alias isStrictlyMonotonic = .isStrictlyMonotonic!(naryFun!less);
129129
}
130130

131-
unittest
131+
@safe pure version(mir_test) unittest
132132
{
133133
import mir.ndslice.algorithm: all;
134134
import mir.ndslice.topology: pairwise;
@@ -145,7 +145,7 @@ unittest
145145
assert(c.pairwise!"a <= b".all);
146146
}
147147

148-
unittest
148+
@safe pure version(mir_test) unittest
149149
{
150150
import mir.ndslice.algorithm: all;
151151
import mir.ndslice.topology: pairwise;
@@ -172,6 +172,12 @@ template sort(alias less = "a < b")
172172
(Slice!(kind, packs, Iterator) slice)
173173
if (packs.length == 1)
174174
{
175+
if (false) // break safety
176+
{
177+
import mir.utility : swapStars;
178+
swapStars(slice._iterator, slice._iterator);
179+
auto l = less(*slice._iterator, *slice._iterator);
180+
}
175181
import mir.ndslice.topology: flattened;
176182
if (slice.anyEmpty)
177183
return slice;
@@ -183,7 +189,7 @@ template sort(alias less = "a < b")
183189
}
184190

185191
///
186-
unittest
192+
@safe pure version(mir_test) unittest
187193
{
188194
import mir.ndslice.algorithm: all;
189195
import mir.ndslice.slice;
@@ -192,12 +198,12 @@ unittest
192198

193199
int[10] arr = [7,1,3,2,9,0,5,4,8,6];
194200

195-
auto data = arr[].ptr.sliced(arr.length);
201+
auto data = arr[].sliced(arr.length);
196202
data.sort();
197203
assert(data.pairwise!"a <= b".all);
198204
}
199205

200-
void quickSortImpl(alias less, Iterator)(Slice!(Contiguous, [1], Iterator) slice)
206+
void quickSortImpl(alias less, Iterator)(Slice!(Contiguous, [1], Iterator) slice) @trusted
201207
{
202208
import mir.utility : swap, swapStars;
203209

@@ -308,7 +314,7 @@ void quickSortImpl(alias less, Iterator)(Slice!(Contiguous, [1], Iterator) slice
308314
}
309315
}
310316

311-
void setPivot(alias less, Iterator)(size_t length, ref Iterator l, ref Iterator mid, ref Iterator r)
317+
void setPivot(alias less, Iterator)(size_t length, ref Iterator l, ref Iterator mid, ref Iterator r) @trusted
312318
{
313319
if (length < 512)
314320
{
@@ -323,7 +329,7 @@ void setPivot(alias less, Iterator)(size_t length, ref Iterator l, ref Iterator
323329
}
324330

325331
void medianOf(alias less, Iterator)
326-
(ref Iterator a, ref Iterator b, ref Iterator c)
332+
(ref Iterator a, ref Iterator b, ref Iterator c) @trusted
327333
{
328334
import mir.utility : swapStars;
329335
if (less(*c, *a)) // c < a
@@ -355,7 +361,7 @@ void medianOf(alias less, Iterator)
355361
}
356362

357363
void medianOf(alias less, Iterator)
358-
(ref Iterator a, ref Iterator b, ref Iterator c, ref Iterator d, ref Iterator e)
364+
(ref Iterator a, ref Iterator b, ref Iterator c, ref Iterator d, ref Iterator e) @trusted
359365
{
360366
import mir.utility : swapStars; // Credit: Teppo Niinimäki
361367
version(unittest) scope(success)

0 commit comments

Comments
 (0)