std.digest: can we get rid of start() functions?

Johannes Pfau nospam at example.com
Wed Oct 10 07:28:36 PDT 2012


Am Wed, 10 Oct 2012 15:02:40 +0200
schrieb Marco Leise <Marco.Leise at gmx.de>:

> Am Wed, 10 Oct 2012 12:55:59 +0200
> schrieb Piotr Szturmaj <bncrbme at jadamspam.pl>:
> 
> > And replace them with global reset function:
> > 
> > void reset(T)(ref T digest)
> >      if (isDigest!T)
> > {
> >      digest = T.init;
> > }
> > 
> > Current usage:
> > 
> > SHA1 sha1;
> > sha1.start();
> > ... calculate hash
> > sha1.start(); // start again
> > ... calculate hash
> > 

There's makeDigest which calls start for you, so you can write code
like this:

auto sha1 = makeDigest!SHA1();
//calculate hash
sha1.start(); //start again
//calculate hash...

But the digests still have to implement a start method, even if it's
only a "this = typeof(this).init;". Maybe we could avoid the start
method by introducing a fallback UFCS start method...

> Unlike classes, structs don't mandate a ctor call, so in
> general you need to disguise the constructor as 'start()'
> 'Create()' or similar, where T.init is not sufficient. If on
> the other hand, you use a ".isInitialized = false" field
> instead of start(), then you'd have to unnecessarily check
> that every time data is added to the digest, instead of doing
> it once. (That might be personal taste though :) )
> 
> I think the constraints are:
> * no start() method ever takes arguments
> * no digest needs to allocate to start
> * no digest depends on global/static variables
> 

Yes, that's the main reason why we have 'start', but it also must work
as a reset function. The difficult question is whether there actually
is a hash algorithm (or whether there will be one in the future) which
needs such a complex start method.

No-allocation often means no C wrapper (although a C API could allow to
use stack objects, but often they just provide opaque structs), so this
is a little controversial. But if we allowed allocation we'd have to
worry about deallocation as well and then things get tricky. Reference
counting is probably still the easiest solution for those cases.



More information about the Digitalmars-d mailing list