Passing a ubyte[] to a function taking a 'ref ubyte[16]'

Johannes Pfau nospam at example.com
Sun Jun 24 00:27:00 PDT 2012


Am Sat, 23 Jun 2012 16:51:14 -0700
schrieb Jonathan M Davis <jmdavisProg at gmx.com>:

> On Saturday, June 23, 2012 20:23:26 Johannes Pfau wrote:
> > I'm working on the new design for std.hash and I hit an interesting
> > problem:
> > 
> > The OOP interface has to take buffers as slices with unknown
> > length, as the length differs between hashes and I have to put a
> > common function declaration in a interface. So I have this:
> > ------------
> > interface Digest
> > {
> >     ubyte[] finish(ubyte[] buf);
> > }
> > ------------
> 
> I confess that I'm baffled as to why you'd even be using interfaces
> for this, given that Phobos always uses structs and templates for
> this sort of thing.

As discussed in the main newsgroup I use both. Interfaces and classes
are useful if you want to change the crypto backend (D/OpenSSL/Windows
Crypto API) at runtime and without breaking the ABI. Regan Heath who
designed the Tango digest module, also said that digets are often used
with the factory pattern, which doesn't work with structs / templates. 

> 
> I believe that that will work, but it's definitely ugly. However, if
> you do that, you _need_ to put an assertion about the length of buf
> in there, otherwise, you could be using memory from past the end of
> buf.
> 
Sure, it's already in the MD5Digest example posted above:
enforce(buf.length >= 16);




More information about the Digitalmars-d-learn mailing list