Thread overview
[Issue 12489] New: std.bitmanip byte swapping routines should be partially instantiable
Mar 29, 2014
Andrej Mitrovic
Mar 29, 2014
Andrej Mitrovic
Mar 29, 2014
Andrej Mitrovic
Mar 30, 2014
Vladimir Panteleev
Mar 30, 2014
Andrej Mitrovic
March 29, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12489

           Summary: std.bitmanip byte swapping routines should be
                    partially instantiable
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-29 12:15:14 CET ---
I've had a use case for generic endianness swapping when reading file headers, and was going to use attributes. For example:

struct Header
{
    @Converter!(littleEndianToNative!ushort)
    ushort reserved1;
}

Unfortunately this doesn't work because you can't partially instantiate littleEndianToNative:

Error: template instance littleEndianToNative!ushort does not match template
declaration littleEndianToNative(T, uint n)(ubyte[n] val) if
(canSwapEndianness!T && n == T.sizeof)

It would have to be converted to a template, e.g.:

Before:

-----
T littleEndianToNative(T, size_t n)(ubyte[n] val) @safe pure nothrow
-----

After:

-----
template littleEndianToNative(T)
{
    T littleEndianToNative(size_t n)(ubyte[n] val) @safe pure nothrow
    {
        ...
    }
}
-----

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 29, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12489



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-29 12:32:30 CET ---
Here's a somewhat generic wrapper workaround:

-----
template PartialTempl(alias templ, T...)
{
    auto PartialTempl(Args...)(auto ref Args args)
    {
        return templ!T(args);
    }
}
-----

Usable as:


-----
@Converter!(PartialTempl!(littleEndianToNative, ushort))
ushort reserved1;
-----

Of course 'littleEndianToNative' and friends take a ubyte[N], not a ushort, so in my file loading routine I have to take that into account. But using attributes is pretty nice.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 29, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12489


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-29 21:01:05 CET ---
(In reply to comment #1)
> Here's a somewhat generic wrapper workaround:
> 
> -----
> template PartialTempl(alias templ, T...)
> {
>     auto PartialTempl(Args...)(auto ref Args args)
>     {
>         return templ!T(args);
>     }
> }
> -----

This solves it for me.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12489


Vladimir Panteleev <thecybershadow@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow@gmail.com


--- Comment #3 from Vladimir Panteleev <thecybershadow@gmail.com> 2014-03-30 14:37:22 EEST ---
(In reply to comment #0)
>     @Converter!(littleEndianToNative!ushort)
>     ushort reserved1;

I don't know the context, but there's some redundancy here already (ushort is listed twice). Any reason you can't use `@Converter!littleEndianToNative` ?

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12489



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-30 14:45:56 CEST ---
(In reply to comment #3)
> (In reply to comment #0)
> >     @Converter!(littleEndianToNative!ushort)
> >     ushort reserved1;
> 
> I don't know the context, but there's some redundancy here already (ushort is listed twice). Any reason you can't use `@Converter!littleEndianToNative` ?

Yeah you're right, I don't need to embed this information. It's really nice that I can now write:

struct Header
{
    @Converter!littleEndianToNative
    {
        ushort reserved1;

        ushort ordnum;

        ushort insnum;

        ushort patnum;

        ushort flags;

        ushort cwtv;

        ushort version_;
    }
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------