@@ -1814,6 +1814,13 @@ if (isCallable!(F))
1814
1814
{
1815
1815
return fp;
1816
1816
}
1817
+ else static if (is (F Func == Func* ) && is (Func == function ))
1818
+ {
1819
+ return function (ref F fp) @trusted
1820
+ {
1821
+ return buildDelegate (fp);
1822
+ }(fp);
1823
+ }
1817
1824
else static if (is (typeof (&F.opCall ) == delegate )
1818
1825
|| (is (typeof (&F.opCall ) V : V* ) && is (V == function )))
1819
1826
{
@@ -1825,6 +1832,27 @@ if (isCallable!(F))
1825
1832
}
1826
1833
else
1827
1834
{
1835
+ static assert (false , " Unsupported type of callable, please open an issue." );
1836
+ }
1837
+ }
1838
+
1839
+ // /
1840
+ @safe unittest
1841
+ {
1842
+ static int inc (ref uint num) {
1843
+ num++ ;
1844
+ return 8675309 ;
1845
+ }
1846
+
1847
+ uint myNum = 0 ;
1848
+ auto incMyNumDel = toDelegate(&inc);
1849
+ auto returnVal = incMyNumDel(myNum);
1850
+ assert (myNum == 1 );
1851
+ }
1852
+
1853
+ private template buildDelegate (F)
1854
+ {
1855
+ auto buildDelegate (auto ref F fp) {
1828
1856
alias DelType = typeof (&(new DelegateFaker! (F)).doIt);
1829
1857
1830
1858
static struct DelegateFields {
@@ -1854,21 +1882,22 @@ if (isCallable!(F))
1854
1882
}
1855
1883
}
1856
1884
1857
- // /
1858
- @system unittest
1885
+ @safe unittest
1859
1886
{
1860
1887
static int inc (ref uint num) {
1861
1888
num++ ;
1862
1889
return 8675309 ;
1863
1890
}
1864
1891
1865
- uint myNum = 0 ;
1866
- auto incMyNumDel = toDelegate(&inc);
1867
- auto returnVal = incMyNumDel(myNum);
1868
- assert (myNum == 1 );
1892
+ uint myNum = 0x1337 ;
1893
+ struct S1 { int opCall () { inc(myNum); return myNum; } }
1894
+ static assert (! is (typeof (&s1.opCall ) == delegate ));
1895
+ S1 s1;
1896
+ auto getvals1 = toDelegate(s1);
1897
+ assert (getvals1() == 0x1338 );
1869
1898
}
1870
1899
1871
- @system unittest // not @safe due to toDelegate
1900
+ @system unittest
1872
1901
{
1873
1902
static int inc (ref uint num) {
1874
1903
num++ ;
@@ -1954,7 +1983,7 @@ if (isCallable!(F))
1954
1983
}
1955
1984
1956
1985
1957
- @system unittest
1986
+ @safe unittest
1958
1987
{
1959
1988
static struct S1 { static void opCall ()() { } }
1960
1989
static struct S2 { static T opCall (T = int )(T x) {return x; } }
@@ -1967,6 +1996,22 @@ if (isCallable!(F))
1967
1996
assert (toDelegate(i2)(0xBED ) == 0xBED );
1968
1997
}
1969
1998
1999
+ @safe unittest
2000
+ {
2001
+ static void fun () @system pure nothrow @nogc
2002
+ {
2003
+ return ;
2004
+ }
2005
+
2006
+ auto fn = &fun;
2007
+ static assert ( is (typeof (fn) == void function () @system pure nothrow @nogc ));
2008
+ static assert (! is (typeof (fn) == void function () @safe pure nothrow @nogc ));
2009
+
2010
+ auto dg = fn.toDelegate();
2011
+ static assert ( is (typeof (dg) == void delegate () @system pure nothrow @nogc ));
2012
+ static assert (! is (typeof (dg) == void delegate () @safe pure nothrow @nogc ));
2013
+ }
2014
+
1970
2015
/**
1971
2016
* Passes the fields of a struct as arguments to a function.
1972
2017
*
0 commit comments